From b2df420433192158fe706191fa9ec6e9dda1d67d Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 1 Dec 2020 21:54:08 -0800 Subject: [PATCH] test(typescript): code in src sub-directory --- .../test/fixtures/src-dir/src/index.ts | 69 +++++++++++++++++++ .../test/fixtures/src-dir/tsconfig.json | 24 +++++++ packages/typescript/test/test.js | 28 ++++++++ 3 files changed, 121 insertions(+) create mode 100644 packages/typescript/test/fixtures/src-dir/src/index.ts create mode 100644 packages/typescript/test/fixtures/src-dir/tsconfig.json diff --git a/packages/typescript/test/fixtures/src-dir/src/index.ts b/packages/typescript/test/fixtures/src-dir/src/index.ts new file mode 100644 index 000000000..a8c3a31e1 --- /dev/null +++ b/packages/typescript/test/fixtures/src-dir/src/index.ts @@ -0,0 +1,69 @@ +type FormDataMap = Map; + +class ReadOnlyFormData { + map: FormDataMap; + + constructor(map: FormDataMap) { + this.map = map; + } + + get(key: string) { + return this.map.get(key)?.[0]; + } + + getAll(key: string) { + return this.map.get(key); + } + + has(key: string) { + return this.map.has(key); + } + + *[Symbol.iterator]() { + for (const [key, value] of this.map) { + for (let i = 0; i < value.length; i += 1) { + yield [key, value[i]]; + } + } + } + + *entries() { + for (const [key, value] of this.map) { + for (let i = 0; i < value.length; i += 1) { + yield [key, value[i]]; + } + } + } + + *keys() { + for (const [key, value] of this.map) { + for (let i = 0; i < value.length; i += 1) { + yield key; + } + } + } + + *values() { + for (const [, value] of this.map) { + for (let i = 0; i < value.length; i += 1) { + yield value; + } + } + } +} + +export default function() { + const map: FormDataMap = new Map(); + + return { + append(key: string, value: string) { + if (map.has(key)) { + (map.get(key) as string[]).push(value); + } else { + map.set(key, [value]); + } + }, + + data: new ReadOnlyFormData(map) + }; +} diff --git a/packages/typescript/test/fixtures/src-dir/tsconfig.json b/packages/typescript/test/fixtures/src-dir/tsconfig.json new file mode 100644 index 000000000..470bff243 --- /dev/null +++ b/packages/typescript/test/fixtures/src-dir/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "lib": ["es2020"], + "outDir": "dist", + "target": "es2020", + + "skipLibCheck": true, + "declaration": true, + + "noEmitOnError": true, + "noErrorTruncation": true, + + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, + + "strict": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": true + }, + "exclude": ["node_modules"] +} diff --git a/packages/typescript/test/test.js b/packages/typescript/test/test.js index bb3e5ddc4..736913ff3 100644 --- a/packages/typescript/test/test.js +++ b/packages/typescript/test/test.js @@ -1181,6 +1181,34 @@ test.serial('picks up on newly included typescript files in watch mode', async ( t.true(usage, 'should contain usage'); }); +// TODO: upgrade TypeScript when there is a release containing the fix for this issue +// https://github.com/microsoft/TypeScript/pull/41811. Then enable this test +// More details at https://github.com/rollup/plugins/issues/287. This fails with the message: +// Unexpected token (Note that you need plugins to import files that are not JavaScript) +test.serial.skip('works when code is in src directory', async (t) => { + const bundle = await rollup({ + input: 'fixtures/src-dir/src/index.ts', + output: [ + { + dir: 'fixtures/src-dir/dist', + format: 'esm' + } + ], + plugins: [ + typescript({ + tsconfig: 'fixtures/src-dir/tsconfig.json' + }) + ], + onwarn + }); + const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/src-dir/dist' }, true); + + t.deepEqual( + output.map((out) => out.fileName), + ['index.js', 'types/index.d.ts', 'types/index.d.ts.map'] + ); +}); + function waitForWatcherEvent(watcher, eventCode) { return new Promise((resolve, reject) => { watcher.on('event', function handleEvent(event) {