-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To run the test without those slow test: `npx ava --match ‘!*[slow]’`.
- Loading branch information
Showing
12 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Emittery = require('../../..'); | ||
|
||
const ee = new Emittery(); | ||
|
||
ee.emit('anEvent'); | ||
ee.emit('anEvent', 'some data'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2017", | ||
"lib": ["es2017"], | ||
"module": "commonjs", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Emittery = require('../../..'); | ||
|
||
const ee = new Emittery(); | ||
|
||
ee.emit('anEvent'); | ||
ee.emit('anEvent', 'some data', 'and more'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Emittery = require('../../..'); | ||
|
||
const ee = new Emittery(); | ||
|
||
ee.on('anEvent', (data, more) => undefined); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2017", | ||
"lib": ["es2017"], | ||
"module": "commonjs", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {readdirSync, statSync} from 'fs'; | ||
import {resolve as _resolve, join, extname, relative} from 'path'; | ||
import pkgUp from 'pkg-dir'; | ||
|
||
import test from 'ava'; | ||
import * as ts from 'typescript'; | ||
|
||
const resolve = _resolve.bind(null, __dirname); | ||
const validExtension = new Set(['.js', '.ts']); | ||
const compilerOptions = { | ||
target: ts.ScriptTarget.ES2017, | ||
module: ts.ModuleKind.CommonJS, | ||
strict: true, | ||
noEmit: true | ||
}; | ||
|
||
test('TS can compile valid Emittery method calls [slow]', assertAllCompile, './fixtures/compiles'); | ||
test('TS warns about invalid Emittery method calls [slow]', assertEachFail, './fixtures/fails'); | ||
|
||
function assertAllCompile(t, srcDir) { | ||
const fileNames = listFiles(resolve(srcDir)); | ||
const errors = compile(fileNames); | ||
|
||
t.is(errors.length, 0, errorMessage(errors)); | ||
} | ||
|
||
function assertEachFail(t, srcDir) { | ||
const fileNames = listFiles(resolve(srcDir)).sort(); | ||
const errors = compile(fileNames); | ||
const filesWithErrors = errors | ||
.map(err => (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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters