From f09e9455edf49215f9dc3da7862c01e50022fb70 Mon Sep 17 00:00:00 2001 From: Damien Lebrun Date: Sat, 9 Dec 2017 14:37:25 +0000 Subject: [PATCH] test Typescript type declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To run the test without those slow test: `npx ava --match ‘!*[slow]’`. --- package.json | 1 + test/fixtures/compiles/emit.ts | 6 ++ test/fixtures/compiles/off.ts | 10 ++++ test/fixtures/compiles/on.ts | 13 +++++ test/fixtures/compiles/tsconfig.json | 11 ++++ test/fixtures/fails/emit-extra.ts | 6 ++ test/fixtures/fails/on-extra.ts | 6 ++ test/fixtures/tsconfig.json | 11 ++++ test/snapshots/types.js.md | 32 +++++++++++ test/snapshots/types.js.snap | Bin 0 -> 385 bytes test/types.js | 79 +++++++++++++++++++++++++++ tsconfig.json | 3 +- 12 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/compiles/emit.ts create mode 100644 test/fixtures/compiles/off.ts create mode 100644 test/fixtures/compiles/on.ts create mode 100644 test/fixtures/compiles/tsconfig.json create mode 100644 test/fixtures/fails/emit-extra.ts create mode 100644 test/fixtures/fails/on-extra.ts create mode 100644 test/fixtures/tsconfig.json create mode 100644 test/snapshots/types.js.md create mode 100644 test/snapshots/types.js.snap create mode 100644 test/types.js diff --git a/package.json b/package.json index d84cbdf..719ecaf 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "codecov": "^3.0.0", "delay": "^2.0.0", "nyc": "^11.3.0", + "pkg-dir": "^2.0.0", "typescript": "^2.6.2", "xo": "*" }, diff --git a/test/fixtures/compiles/emit.ts b/test/fixtures/compiles/emit.ts new file mode 100644 index 0000000..60a75e0 --- /dev/null +++ b/test/fixtures/compiles/emit.ts @@ -0,0 +1,6 @@ +import Emittery = require('../../..'); + +const ee = new Emittery(); + +ee.emit('anEvent'); +ee.emit('anEvent', 'some data'); diff --git a/test/fixtures/compiles/off.ts b/test/fixtures/compiles/off.ts new file mode 100644 index 0000000..e0d7a82 --- /dev/null +++ b/test/fixtures/compiles/off.ts @@ -0,0 +1,10 @@ +import Emittery = require('../../..'); + +const ee = new Emittery(); +const listener = () => undefined; + +ee.off('anEvent', () => undefined); +ee.off('anEvent', () => Promise.resolve()); + +ee.off('anEvent', data => undefined); +ee.off('anEvent', data => Promise.resolve()); diff --git a/test/fixtures/compiles/on.ts b/test/fixtures/compiles/on.ts new file mode 100644 index 0000000..97cfcef --- /dev/null +++ b/test/fixtures/compiles/on.ts @@ -0,0 +1,13 @@ +import Emittery = require('../../..'); + +const ee = new Emittery(); + +ee.on('anEvent', () => undefined); +ee.on('anEvent', () => Promise.resolve()); + +ee.on('anEvent', data => undefined); +ee.on('anEvent', data => Promise.resolve()); + +const off = ee.on('anEvent', () => undefined); + +off(); diff --git a/test/fixtures/compiles/tsconfig.json b/test/fixtures/compiles/tsconfig.json new file mode 100644 index 0000000..27a6731 --- /dev/null +++ b/test/fixtures/compiles/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es2017", + "lib": ["es2017"], + "module": "commonjs", + "strict": true, + "moduleResolution": "node", + "allowSyntheticDefaultImports": true + }, + "include": ["*.ts"] +} diff --git a/test/fixtures/fails/emit-extra.ts b/test/fixtures/fails/emit-extra.ts new file mode 100644 index 0000000..4eb97c6 --- /dev/null +++ b/test/fixtures/fails/emit-extra.ts @@ -0,0 +1,6 @@ +import Emittery = require('../../..'); + +const ee = new Emittery(); + +ee.emit('anEvent'); +ee.emit('anEvent', 'some data', 'and more'); diff --git a/test/fixtures/fails/on-extra.ts b/test/fixtures/fails/on-extra.ts new file mode 100644 index 0000000..6a90bbe --- /dev/null +++ b/test/fixtures/fails/on-extra.ts @@ -0,0 +1,6 @@ +import Emittery = require('../../..'); + +const ee = new Emittery(); + +ee.on('anEvent', (data, more) => undefined); + diff --git a/test/fixtures/tsconfig.json b/test/fixtures/tsconfig.json new file mode 100644 index 0000000..27a6731 --- /dev/null +++ b/test/fixtures/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es2017", + "lib": ["es2017"], + "module": "commonjs", + "strict": true, + "moduleResolution": "node", + "allowSyntheticDefaultImports": true + }, + "include": ["*.ts"] +} diff --git a/test/snapshots/types.js.md b/test/snapshots/types.js.md new file mode 100644 index 0000000..ea5c7e6 --- /dev/null +++ b/test/snapshots/types.js.md @@ -0,0 +1,32 @@ +# Snapshot report for `test/types.js` + +The actual snapshot is saved in `types.js.snap`. + +Generated by [AVA](https://ava.li). + +## Type definition disallows invalid Emittery method calls to compile + +> Snapshot 1 + + `test/fixtures/fails/emit-extra.ts (6,1): Expected 1-2 arguments, but got 3.␊ + test/fixtures/fails/on-extra.ts (5,18): Argument of type '(data: any, more: any) => undefined' is not assignable to parameter of type '() => any'.␊ + test/fixtures/fails/on-extra.ts (5,19): Parameter 'data' implicitly has an 'any' type.␊ + test/fixtures/fails/on-extra.ts (5,25): Parameter 'more' implicitly has an 'any' type. + +## Type definition disallows invalid Emittery method calls to compile [slow] + +> Snapshot 1 + + `test/fixtures/fails/emit-extra.ts (6,1): Expected 1-2 arguments, but got 3.␊ + test/fixtures/fails/on-extra.ts (5,18): Argument of type '(data: any, more: any) => undefined' is not assignable to parameter of type '() => any'.␊ + test/fixtures/fails/on-extra.ts (5,19): Parameter 'data' implicitly has an 'any' type.␊ + test/fixtures/fails/on-extra.ts (5,25): Parameter 'more' implicitly has an 'any' type. + +## TS warns about invalid Emittery method calls [slow] + +> Snapshot 1 + + `test/fixtures/fails/emit-extra.ts (6,1): Expected 1-2 arguments, but got 3.␊ + test/fixtures/fails/on-extra.ts (5,18): Argument of type '(data: any, more: any) => undefined' is not assignable to parameter of type '(eventData?: any) => any'.␊ + test/fixtures/fails/on-extra.ts (5,19): Parameter 'data' implicitly has an 'any' type.␊ + test/fixtures/fails/on-extra.ts (5,25): Parameter 'more' implicitly has an 'any' type.` \ No newline at end of file diff --git a/test/snapshots/types.js.snap b/test/snapshots/types.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..e7adc5e0395edd79b87bdc453f9003ee3cf9cdf4 GIT binary patch literal 385 zcmV-{0e=2LRzVDW#J_hJ<(uNRR4{@?AFwbmFfp)$^)s?C2r>0AmZTP!=%-~?l#~{w7VD=a zX66*@r{-ps=%!Yb6ea4F6f0<$=@@ESDY#Y?q$Zc7rYIQd8Yv_erI+TW=9LudC?u7Z zD5U3?C>ZN;VKXZ~56L7`9YYJCQI1gE3i)XYC6xuK3hEjui6x0v3W<4@Itsb@MX6w> zrh=`VLTO$~YFcJqYKpo-X0bvZ(4xfR;>`5C#H5^5g_3-Qg2bZ4+|-iPA|%7W8i59= z6XzsLppydN#;Ajw0<X&jifSy#TLg?9jhdkm fHM56h)TEXH4R-;WXpb;w2*wlu=bysJGz9 (err.file ? err.file.fileName : null)) + .filter(Boolean) + .filter(unique); + + t.deepEqual(filesWithErrors, fileNames, 'Some file did not emit any compile error.'); + t.snapshot(errorMessage(errors)); +} + +function listFiles(srcRoot) { + return readdirSync(srcRoot) + .filter(hasValidExtension) + .map(name => join(srcRoot, name)) + .filter(isFile); +} + +function compile(fileNames, options = compilerOptions) { + const program = ts.createProgram(fileNames, options); + const emitResult = program.emit(); + + return ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); +} + +function errorMessage(diagnosticList) { + const root = pkgUp.sync(); + + return diagnosticList.map(diagnostic => { + if (!diagnostic.file) { + return `${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`; + } + + const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + const fileName = relative(root, diagnostic.file.fileName); + + return `${fileName} (${line + 1},${character + 1}): ${message}`; + }).join('\n'); +} + +function isFile(path) { + return statSync(path).isFile(); +} + +function hasValidExtension(path) { + return validExtension.has(extname(path)); +} + +function unique(value, index, list) { + return list.indexOf(value) === index; +} diff --git a/tsconfig.json b/tsconfig.json index 7d1827a..54da939 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,5 +8,6 @@ "allowSyntheticDefaultImports": true, "allowJs": true }, - "include": ["**/*.ts"] + "include": ["**/*"], + "exclude": ["test/fixtures/**/*"] }