0

I am defining and exporting an inteface in one file but can't import it in a test case of mocha. I am getting Cannot use import statement outside a module. I tried to add "type" : "module" in package.json but it didn't help.

Do you have any ideas what I am missing?

Thanks in advance for your help.

ReadonlyArray.ts

export interface ImmutableList<T> {
    readonly [n: number]: T;
}

product.test.ts

import {ImmutableList} from "../../../Utils/ReadonlyArray";
const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();
describe('', () => {
    it('', () => {
        const array = [1, 2, 3];
        let rdArray: ImmutableList<number> = [];
        for (let item of array){
            console.log(item);
        }
    });
});

package.json

{
  "name": "wsep",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "tsc test/product.test.ts -o test/product.test.js && mocha test/product.test.js --require ts-node/register test/product.test.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "simplr-logger": "^1.0.1"
  },
  "devDependencies": {
    "@types/mocha": "^8.2.2",
    "@types/node": "^14.14.37",
    "chai": "^4.3.4",
    "mocha": "^8.3.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "baseUrl": "./",
    "outDir": "./build",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "allowJs": true,
    "incremental": true
  },
  "exclude": ["node_modules", "build"]
}
  • Does this answer your question? [react create app, typescript unit test with mocha and chai what is the correct setup to support es6 modules?](https://stackoverflow.com/questions/66972754/react-create-app-typescript-unit-test-with-mocha-and-chai-what-is-the-correct-s) – lifeisfoo Apr 09 '21 at 08:01
  • @lifeisfoo Thanks a lot. That reference solved it. You saved me. <3 – Mark Oulitin Apr 09 '21 at 15:49

0 Answers0