From 100ad98407175ff08ce98b6402e9dc89c28200ba Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 24 Aug 2020 16:16:35 +0800 Subject: [PATCH] refactor(test_module): migrate tests to TypeScript --- .eslintrc.yml | 16 ++--- package.json | 28 ++++++-- .../{buffer.spec.js => buffer.spec.ts} | 2 +- .../__test__/{class.spec.js => class.spec.ts} | 2 +- ...ternal.spec.js => create-external.spec.ts} | 2 +- .../{either.spec.js => either.spec.ts} | 2 +- .../{function.spec.js => function.spec.ts} | 6 +- ...rsion.spec.js => get-napi-version.spec.ts} | 3 +- .../{js-value.spec.js => js-value.spec.ts} | 2 +- test_module/__test__/napi-version.js | 3 - test_module/__test__/napi-version.ts | 2 + ...on.spec.js => threadsafe_function.spec.ts} | 7 +- ...eadfile.spec.js => tokio_readfile.spec.ts} | 12 ++-- ...olate.spec.js => tokio_rt-isolate.spec.ts} | 7 +- .../{tokio_rt.spec.js => tokio_rt.spec.ts} | 10 +-- ...{tsfn_error.spec.js => tsfn_error.spec.ts} | 7 +- .../__test__/napi4/{uv.spec.js => uv.spec.ts} | 14 ++-- test_module/__test__/napi4/uv_worker.js | 9 ++- .../{is-date.spec.js => is-date.spec.ts} | 5 +- .../napi6/{bigint.spec.js => bigint.spec.ts} | 5 +- .../__test__/{spawn.spec.js => spawn.spec.ts} | 2 +- .../{string.spec.js => string.spec.ts} | 2 +- test_module/__test__/string.spec.ts.md | 11 +++ test_module/__test__/string.spec.ts.snap | Bin 0 -> 140 bytes .../{symbol.spec.js => symbol.spec.ts} | 2 +- .../__test__/{throw.spec.js => throw.spec.ts} | 2 +- test_module/tsconfig.json | 8 --- tsconfig.json | 1 - yarn.lock | 64 +++++++++++++++++- 29 files changed, 161 insertions(+), 75 deletions(-) rename test_module/__test__/{buffer.spec.js => buffer.spec.ts} (92%) rename test_module/__test__/{class.spec.js => class.spec.ts} (91%) rename test_module/__test__/{create-external.spec.js => create-external.spec.ts} (89%) rename test_module/__test__/{either.spec.js => either.spec.ts} (92%) rename test_module/__test__/{function.spec.js => function.spec.ts} (58%) rename test_module/__test__/{get-napi-version.spec.js => get-napi-version.spec.ts} (83%) rename test_module/__test__/{js-value.spec.js => js-value.spec.ts} (98%) delete mode 100644 test_module/__test__/napi-version.js create mode 100644 test_module/__test__/napi-version.ts rename test_module/__test__/napi4/{threadsafe_function.spec.js => threadsafe_function.spec.ts} (78%) rename test_module/__test__/napi4/{tokio_readfile.spec.js => tokio_readfile.spec.ts} (73%) rename test_module/__test__/napi4/{tokio_rt-isolate.spec.js => tokio_rt-isolate.spec.ts} (84%) rename test_module/__test__/napi4/{tokio_rt.spec.js => tokio_rt.spec.ts} (91%) rename test_module/__test__/napi4/{tsfn_error.spec.js => tsfn_error.spec.ts} (78%) rename test_module/__test__/napi4/{uv.spec.js => uv.spec.ts} (81%) rename test_module/__test__/napi5/{is-date.spec.js => is-date.spec.ts} (88%) rename test_module/__test__/napi6/{bigint.spec.js => bigint.spec.ts} (91%) rename test_module/__test__/{spawn.spec.js => spawn.spec.ts} (87%) rename test_module/__test__/{string.spec.js => string.spec.ts} (87%) create mode 100644 test_module/__test__/string.spec.ts.md create mode 100644 test_module/__test__/string.spec.ts.snap rename test_module/__test__/{symbol.spec.js => symbol.spec.ts} (95%) rename test_module/__test__/{throw.spec.js => throw.spec.ts} (94%) delete mode 100644 test_module/tsconfig.json diff --git a/.eslintrc.yml b/.eslintrc.yml index 3ff3d348..ef17a47d 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -14,11 +14,15 @@ env: plugins: - import + - '@typescript-eslint' extends: - eslint:recommended - plugin:prettier/recommended +globals: + BigInt: 'readonly' + rules: # 0 = off, 1 = warn, 2 = error 'space-before-function-paren': 0 @@ -200,15 +204,3 @@ rules: ], }, ] - -overrides: - - files: - - ./test_module/**/*.js - plugins: - - '@typescript-eslint' - parserOptions: - project: ./test_module/tsconfig.json - rules: - '@typescript-eslint/prefer-nullish-coalescing': 0 - '@typescript-eslint/prefer-optional-chain': 0 - '@typescript-eslint/no-non-null-asserted-optional-chain': 0 diff --git a/package.json b/package.json index 9733e3c0..21bae0e7 100644 --- a/package.json +++ b/package.json @@ -44,15 +44,30 @@ "trailingComma": "all", "arrowParens": "always" }, - "files": ["scripts", "LICENSE"], + "files": [ + "scripts", + "LICENSE" + ], "lint-staged": { - "*.js": ["prettier --write"], - "*.@(yml|yaml)": ["prettier --parser yaml --write"], - "*.json": ["prettier --parser json --write"], - "*.md": ["prettier --parser markdown --write"] + "*.js": [ + "prettier --write" + ], + "*.@(yml|yaml)": [ + "prettier --parser yaml --write" + ], + "*.json": [ + "prettier --parser json --write" + ], + "*.md": [ + "prettier --parser markdown --write" + ] }, "ava": { - "files": ["test_module/__test__/**/*.spec.js"] + "extensions": ["ts", "tsx"], + "files": [ + "test_module/__test__/**/*.spec.ts" + ], + "require": ["@swc-node/register"] }, "husky": { "hooks": { @@ -61,6 +76,7 @@ }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.1", + "@swc-node/register": "^0.4.1", "@types/inquirer": "^7.3.0", "@types/node": "^14.6.0", "@typescript-eslint/eslint-plugin": "^3.9.1", diff --git a/test_module/__test__/buffer.spec.js b/test_module/__test__/buffer.spec.ts similarity index 92% rename from test_module/__test__/buffer.spec.js rename to test_module/__test__/buffer.spec.ts index c44305df..eb5a61fa 100644 --- a/test_module/__test__/buffer.spec.js +++ b/test_module/__test__/buffer.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/class.spec.js b/test_module/__test__/class.spec.ts similarity index 91% rename from test_module/__test__/class.spec.js rename to test_module/__test__/class.spec.ts index a506e663..1d7acf07 100644 --- a/test_module/__test__/class.spec.js +++ b/test_module/__test__/class.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/create-external.spec.js b/test_module/__test__/create-external.spec.ts similarity index 89% rename from test_module/__test__/create-external.spec.js rename to test_module/__test__/create-external.spec.ts index 8bc60eaa..1e2cc19f 100644 --- a/test_module/__test__/create-external.spec.js +++ b/test_module/__test__/create-external.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/either.spec.js b/test_module/__test__/either.spec.ts similarity index 92% rename from test_module/__test__/either.spec.js rename to test_module/__test__/either.spec.ts index ce9cd522..032fb543 100644 --- a/test_module/__test__/either.spec.js +++ b/test_module/__test__/either.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/function.spec.js b/test_module/__test__/function.spec.ts similarity index 58% rename from test_module/__test__/function.spec.js rename to test_module/__test__/function.spec.ts index d23585cc..7843dcd5 100644 --- a/test_module/__test__/function.spec.js +++ b/test_module/__test__/function.spec.ts @@ -1,16 +1,16 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') test('should call the function', (t) => { - bindings.testCallFunction((arg1, arg2) => { + bindings.testCallFunction((arg1: string, arg2: string) => { t.is(`${arg1} ${arg2}`, 'hello world') }) }) test('should set "this" properly', (t) => { const obj = {} - bindings.testCallFunctionWithThis.call(obj, function () { + bindings.testCallFunctionWithThis.call(obj, function (this: typeof obj) { t.is(this, obj) }) }) diff --git a/test_module/__test__/get-napi-version.spec.js b/test_module/__test__/get-napi-version.spec.ts similarity index 83% rename from test_module/__test__/get-napi-version.spec.js rename to test_module/__test__/get-napi-version.spec.ts index 42a8eccc..2e51c1a5 100644 --- a/test_module/__test__/get-napi-version.spec.js +++ b/test_module/__test__/get-napi-version.spec.ts @@ -1,9 +1,10 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') test('should get napi version', (t) => { const napiVersion = bindings.getNapiVersion() t.true(typeof napiVersion === 'number') + // @ts-expect-error t.is(`${napiVersion}`, process.versions.napi) }) diff --git a/test_module/__test__/js-value.spec.js b/test_module/__test__/js-value.spec.ts similarity index 98% rename from test_module/__test__/js-value.spec.js rename to test_module/__test__/js-value.spec.ts index fa727bca..402da262 100644 --- a/test_module/__test__/js-value.spec.js +++ b/test_module/__test__/js-value.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/napi-version.js b/test_module/__test__/napi-version.js deleted file mode 100644 index 203f6ee0..00000000 --- a/test_module/__test__/napi-version.js +++ /dev/null @@ -1,3 +0,0 @@ -const napi = parseInt(process.versions.napi || '1', 10) - -module.exports = napi diff --git a/test_module/__test__/napi-version.ts b/test_module/__test__/napi-version.ts new file mode 100644 index 00000000..2d277819 --- /dev/null +++ b/test_module/__test__/napi-version.ts @@ -0,0 +1,2 @@ +// @ts-expect-error +export const napiVersion = parseInt(process.versions.napi || '1', 10) diff --git a/test_module/__test__/napi4/threadsafe_function.spec.js b/test_module/__test__/napi4/threadsafe_function.spec.ts similarity index 78% rename from test_module/__test__/napi4/threadsafe_function.spec.js rename to test_module/__test__/napi4/threadsafe_function.spec.ts index 418db223..0565992c 100644 --- a/test_module/__test__/napi4/threadsafe_function.spec.js +++ b/test_module/__test__/napi4/threadsafe_function.spec.ts @@ -1,7 +1,8 @@ -const test = require('ava') +import test from 'ava' + +import { napiVersion } from '../napi-version' const bindings = require('../../index.node') -const napiVersion = require('../napi-version') test('should get js function called from a thread', async (t) => { let called = 0 @@ -12,7 +13,7 @@ test('should get js function called from a thread', async (t) => { } return new Promise((resolve, reject) => { - bindings.testThreadsafeFunction((...args) => { + bindings.testThreadsafeFunction((...args: any[]) => { called += 1 try { t.deepEqual(args, [null, 42, 1, 2, 3]) diff --git a/test_module/__test__/napi4/tokio_readfile.spec.js b/test_module/__test__/napi4/tokio_readfile.spec.ts similarity index 73% rename from test_module/__test__/napi4/tokio_readfile.spec.js rename to test_module/__test__/napi4/tokio_readfile.spec.ts index 6c39c027..0970c8ca 100644 --- a/test_module/__test__/napi4/tokio_readfile.spec.js +++ b/test_module/__test__/napi4/tokio_readfile.spec.ts @@ -1,9 +1,11 @@ -const test = require('ava') -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' + +import test from 'ava' + +import { napiVersion } from '../napi-version' const bindings = require('../../index.node') -const napiVersion = require('../napi-version') const filepath = path.resolve(__dirname, './example.txt') @@ -13,7 +15,7 @@ test('should read a file and return its a buffer', async (t) => { return } return new Promise((resolve, reject) => { - bindings.testTokioReadfile(filepath, (err, value) => { + bindings.testTokioReadfile(filepath, (err: Error | null, value: Buffer) => { try { t.is(err, null) t.is(Buffer.isBuffer(value), true) diff --git a/test_module/__test__/napi4/tokio_rt-isolate.spec.js b/test_module/__test__/napi4/tokio_rt-isolate.spec.ts similarity index 84% rename from test_module/__test__/napi4/tokio_rt-isolate.spec.js rename to test_module/__test__/napi4/tokio_rt-isolate.spec.ts index ea01323c..2373288f 100644 --- a/test_module/__test__/napi4/tokio_rt-isolate.spec.js +++ b/test_module/__test__/napi4/tokio_rt-isolate.spec.ts @@ -1,7 +1,8 @@ -const test = require('ava') -const { join } = require('path') +import { join } from 'path' -const napiVersion = require('../napi-version') +import test from 'ava' + +import { napiVersion } from '../napi-version' const filepath = join(__dirname, './example.txt') diff --git a/test_module/__test__/napi4/tokio_rt.spec.js b/test_module/__test__/napi4/tokio_rt.spec.ts similarity index 91% rename from test_module/__test__/napi4/tokio_rt.spec.js rename to test_module/__test__/napi4/tokio_rt.spec.ts index 1873e550..ba3a7151 100644 --- a/test_module/__test__/napi4/tokio_rt.spec.js +++ b/test_module/__test__/napi4/tokio_rt.spec.ts @@ -1,9 +1,11 @@ -const test = require('ava') -const { join } = require('path') -const { readFileSync } = require('fs') +import { readFileSync } from 'fs' +import { join } from 'path' + +import test from 'ava' + +import { napiVersion } from '../napi-version' const bindings = require('../../index.node') -const napiVersion = require('../napi-version') const filepath = join(__dirname, './example.txt') diff --git a/test_module/__test__/napi4/tsfn_error.spec.js b/test_module/__test__/napi4/tsfn_error.spec.ts similarity index 78% rename from test_module/__test__/napi4/tsfn_error.spec.js rename to test_module/__test__/napi4/tsfn_error.spec.ts index ad5a5468..fc1246b0 100644 --- a/test_module/__test__/napi4/tsfn_error.spec.js +++ b/test_module/__test__/napi4/tsfn_error.spec.ts @@ -1,7 +1,8 @@ -const test = require('ava') +import test from 'ava' + +import { napiVersion } from '../napi-version' const bindings = require('../../index.node') -const napiVersion = require('../napi-version') test('should call callback with the first arguments as an Error', async (t) => { if (napiVersion < 4) { @@ -9,7 +10,7 @@ test('should call callback with the first arguments as an Error', async (t) => { return } return new Promise((resolve, reject) => { - bindings.testTsfnError((err) => { + bindings.testTsfnError((err: Error) => { try { t.is(err instanceof Error, true) t.is(err.message, 'invalid') diff --git a/test_module/__test__/napi4/uv.spec.js b/test_module/__test__/napi4/uv.spec.ts similarity index 81% rename from test_module/__test__/napi4/uv.spec.js rename to test_module/__test__/napi4/uv.spec.ts index 639e4dc6..b13c2e8d 100644 --- a/test_module/__test__/napi4/uv.spec.js +++ b/test_module/__test__/napi4/uv.spec.ts @@ -1,11 +1,13 @@ -const test = require('ava') -const { join, resolve } = require('path') -const { readFileSync } = require('fs') +import { readFileSync } from 'fs' +import { join, resolve } from 'path' + +import test from 'ava' + +import { napiVersion } from '../napi-version' const bindings = require('../../index.node') -const napiVersion = require('../napi-version') -let threadMod +let threadMod: any try { threadMod = require('worker_threads') @@ -33,7 +35,7 @@ if (threadMod && napiVersion >= 4) { const script = resolve(__dirname, './uv_worker.js') const worker = new Worker(script) const success = await new Promise((resolve) => { - worker.on('message', (success) => { + worker.on('message', (success: boolean) => { resolve(success) }) }) diff --git a/test_module/__test__/napi4/uv_worker.js b/test_module/__test__/napi4/uv_worker.js index dab37c10..db2945c7 100644 --- a/test_module/__test__/napi4/uv_worker.js +++ b/test_module/__test__/napi4/uv_worker.js @@ -1,6 +1,7 @@ -const { isMainThread, parentPort } = require('worker_threads') -const { join } = require('path') const { readFileSync } = require('fs') +const { join } = require('path') +const { isMainThread, parentPort } = require('worker_threads') + const bindings = require('../../index.node') const filepath = join(__dirname, './example.txt') @@ -12,5 +13,7 @@ if (!isMainThread) { Buffer.isBuffer(fileContent) && readFileSync(filepath).toString('utf8') === fileContent.toString('utf8') parentPort.postMessage(success) - })() + })().catch((e) => { + throw e + }) } diff --git a/test_module/__test__/napi5/is-date.spec.js b/test_module/__test__/napi5/is-date.spec.ts similarity index 88% rename from test_module/__test__/napi5/is-date.spec.js rename to test_module/__test__/napi5/is-date.spec.ts index 736bb59c..e27fb186 100644 --- a/test_module/__test__/napi5/is-date.spec.js +++ b/test_module/__test__/napi5/is-date.spec.ts @@ -1,6 +1,7 @@ -const test = require('ava') +import test from 'ava' + +import { napiVersion } from '../napi-version' -const napiVersion = require('../napi-version') const bindings = require('../../index.node') test('should return false if value is not date', (t) => { diff --git a/test_module/__test__/napi6/bigint.spec.js b/test_module/__test__/napi6/bigint.spec.ts similarity index 91% rename from test_module/__test__/napi6/bigint.spec.js rename to test_module/__test__/napi6/bigint.spec.ts index a462f7f1..6d88ae32 100644 --- a/test_module/__test__/napi6/bigint.spec.js +++ b/test_module/__test__/napi6/bigint.spec.ts @@ -1,6 +1,7 @@ -const test = require('ava') +import test from 'ava' + +import { napiVersion } from '../napi-version' -const napiVersion = require('../napi-version') const bindings = require('../../index.node') test('should create bigints', (t) => { diff --git a/test_module/__test__/spawn.spec.js b/test_module/__test__/spawn.spec.ts similarity index 87% rename from test_module/__test__/spawn.spec.js rename to test_module/__test__/spawn.spec.ts index 2d73d0c4..41ffd065 100644 --- a/test_module/__test__/spawn.spec.js +++ b/test_module/__test__/spawn.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/string.spec.js b/test_module/__test__/string.spec.ts similarity index 87% rename from test_module/__test__/string.spec.js rename to test_module/__test__/string.spec.ts index 563191bf..9af2c008 100644 --- a/test_module/__test__/string.spec.js +++ b/test_module/__test__/string.spec.ts @@ -1,4 +1,4 @@ -const test = require('ava') +import test from 'ava' const bindings = require('../index.node') diff --git a/test_module/__test__/string.spec.ts.md b/test_module/__test__/string.spec.ts.md new file mode 100644 index 00000000..ecb76314 --- /dev/null +++ b/test_module/__test__/string.spec.ts.md @@ -0,0 +1,11 @@ +# Snapshot report for `test_module/__test__/string.spec.ts` + +The actual snapshot is saved in `string.spec.ts.snap`. + +Generated by [AVA](https://avajs.dev). + +## should be able to concat string + +> Snapshot 1 + + 'JavaScript 🌳 你好 napi + Rust 🦀 string!' diff --git a/test_module/__test__/string.spec.ts.snap b/test_module/__test__/string.spec.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..624728bff366ce5dd2461b5eacff9e8648fc8738 GIT binary patch literal 140 zcmZ<^b5sb4$#okJaxu9oQ0v*EAxIV&