wechaty / matrix-appservice

Wechaty [Matrix] Application Services Bridge
https://matrix.org/docs/projects/bridge/matrix-appservice-wechaty
Apache License 2.0
131 stars 16 forks source link

Cannot use import statement outside a module in .spec.ts files in unit test. #102

Closed lprintf closed 2 years ago

lprintf commented 3 years ago

After update tsconfig to 0.20.2 in PR #97 encounter a problem about unit test.

logs:detail

> matrix-appservice-wechaty@0.9.5 test:unit
> blue-tape -r ts-node/register 'src/**/*.spec.ts' 'tests/**/*.spec.ts'

/workspace/src/appservice-manager.spec.ts:2
import { test } from 'tstest';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Module.m._compile (/workspace/node_modules/ts-node/src/index.ts:1310:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/workspace/node_modules/ts-node/src/index.ts:1313:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
lprintf commented 3 years ago

It work well in @chatie/tsconfig 0.16.2. Should we give up upgrading the dependency? @huan

huan commented 3 years ago

Please refer to https://github.com/Chatie/tsconfig/issues/16 to see how to make the repo work with the esm.

In short:

  1. You need to add "type": "module" to package.json
  2. You need to use import './config.js' instead of import './config'
  3. Rename .eslint.js to .eslint.cjs
lprintf commented 2 years ago

fix in #97