refactor(test_module): migrate tests to TypeScript

This commit is contained in:
LongYinan 2020-08-24 16:16:35 +08:00
parent 98210cd771
commit 100ad98407
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
29 changed files with 161 additions and 75 deletions

View file

@ -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

View file

@ -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",

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -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)
})
})

View file

@ -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)
})

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,3 +0,0 @@
const napi = parseInt(process.versions.napi || '1', 10)
module.exports = napi

View file

@ -0,0 +1,2 @@
// @ts-expect-error
export const napiVersion = parseInt(process.versions.napi || '1', 10)

View file

@ -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])

View file

@ -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)

View file

@ -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')

View file

@ -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')

View file

@ -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')

View file

@ -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)
})
})

View file

@ -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
})
}

View file

@ -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) => {

View file

@ -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) => {

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -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!'

Binary file not shown.

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,4 +1,4 @@
const test = require('ava')
import test from 'ava'
const bindings = require('../index.node')

View file

@ -1,8 +0,0 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"allowJs": true
},
"include": ["."]
}

View file

@ -39,6 +39,5 @@
"esnext"
]
},
"include": ["./src"],
"exclude": ["node_modules"]
}

View file

@ -213,6 +213,13 @@
resolved "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd"
integrity sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==
"@node-rs/helper@^0.3.0":
version "0.3.0"
resolved "https://registry.npmjs.org/@node-rs/helper/-/helper-0.3.0.tgz#6ab53f3842086f56df92f4990dda642afba84f10"
integrity sha512-1p67WcdFAtz5jVGpzpqJL2W+JKmoW4jmPD2cIJOAU4U+Fl5G9QsoFTa07ctiNosSsjYqnvgyE0KWV75YdpDqDg==
dependencies:
tslib "^2.0.0"
"@nodelib/fs.scandir@2.1.3":
version "2.1.3"
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
@ -239,6 +246,49 @@
resolved "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
integrity sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ==
"@swc-node/core-darwin@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@swc-node/core-darwin/-/core-darwin-0.4.1.tgz#e3356da01e2c40af3594ade3d8413034d9bf6732"
integrity sha512-jniCmDu9cp/zZua2z34hALb7ommf/Q7UtSLNuTfkJ2MZFFpSUQ8bBrTjfk18Oql3z+uKGG0vKOsZhatT4yWBpg==
"@swc-node/core-linux@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@swc-node/core-linux/-/core-linux-0.4.1.tgz#43dcb1f2d3e7c3324aaada894a01ac1830dab4d0"
integrity sha512-5djnpKD6vAEkjgO2iFhbmsVJz5cxLO4ELJBAkvESybQO4Ts4kXkKjGbyJ7hpBCi2m9LMc2t45znb+sqlU5/8HA==
"@swc-node/core-win32@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@swc-node/core-win32/-/core-win32-0.4.1.tgz#1869528c31c1db2dd350422346070a4c01bf1b99"
integrity sha512-puuCwkMkp8GJRaZgHadseKt6z2K58wbozCaYoWdJ4B7ULOD1DQj92QI2AEyivxyUBD+BUGrSVUE1ICUK06E6Hg==
"@swc-node/core@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@swc-node/core/-/core-0.4.1.tgz#69f444d0f3d54d612c2195d84e7fcb2b18191711"
integrity sha512-Oz/EoVd/XY3f+YcKEIHs3hU/Qe8Q6yigU1MEs0stVYR1QlNnupAKV1qw6PqsVni6MAW/HN6QlBnEyVzqX5Osew==
dependencies:
"@node-rs/helper" "^0.3.0"
optionalDependencies:
"@swc-node/core-darwin" "^0.4.1"
"@swc-node/core-linux" "^0.4.1"
"@swc-node/core-win32" "^0.4.1"
"@swc-node/register@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@swc-node/register/-/register-0.4.1.tgz#e66317fc13d7167dbb9256664f93286b8aca9c2a"
integrity sha512-G/sjCav5soUOQX+b+JrorKkR7fmHJ2oXg68zguKqdQtTlCLXQt5zzX1QROphej53rtQlZ1Uajvdil/9NwFsJGg==
dependencies:
"@swc-node/core" "^0.4.1"
"@swc-node/sourcemap-support" "^0.1.7"
debug "^4.1.1"
pirates "^4.0.1"
"@swc-node/sourcemap-support@^0.1.7":
version "0.1.7"
resolved "https://registry.npmjs.org/@swc-node/sourcemap-support/-/sourcemap-support-0.1.7.tgz#2e9b94e0bb7735f4b1ce0f0a464325dd0ad25465"
integrity sha512-tC/h/JJi5WJuLbDbWt2aKFLyafUrbSnRwcpO89jmQIVtQUssbfoXGupoGrPThzCfrJyXUNNflWxl3SxIdCvPMA==
dependencies:
source-map-support "^0.5.19"
"@szmarczak/http-timer@^1.1.2":
version "1.1.2"
resolved "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421"
@ -2377,6 +2427,11 @@ nice-try@^1.0.4:
resolved "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
node-modules-regexp@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40"
integrity sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA=
node-preload@^0.2.1:
version "0.2.1"
resolved "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz#c03043bb327f417a18fee7ab7ee57b408a144301"
@ -2739,6 +2794,13 @@ pify@^4.0.1:
resolved "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231"
integrity sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==
pirates@^4.0.1:
version "4.0.1"
resolved "https://registry.npmjs.org/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
integrity sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA==
dependencies:
node-modules-regexp "^1.0.0"
pkg-conf@^3.1.0:
version "3.1.0"
resolved "https://registry.npmjs.org/pkg-conf/-/pkg-conf-3.1.0.tgz#d9f9c75ea1bae0e77938cde045b276dac7cc69ae"
@ -3447,7 +3509,7 @@ tslib@^1.8.1, tslib@^1.9.0:
resolved "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==
tslib@^2.0.1:
tslib@^2.0.0, tslib@^2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.0.1.tgz#410eb0d113e5b6356490eec749603725b021b43e"
integrity sha512-SgIkNheinmEBgx1IUNirK0TUD4X9yjjBRTqqjggWCU3pUEqIk3/Uwl3yRixYKT6WjQuGiwDv4NomL3wqRCj+CQ==