Ava: import test -> SyntaxError: Unexpected identifier

Created on 15 May 2019  路  7Comments  路  Source: avajs/ava

Description

Basic installation fails on ava test with typescript is giving me the below stacktrace.

Test Source

import test from 'ava'

test('one plus two equals three', t => {
  t.is(1 + 2, 3)
})

Error Message & Stack Trace

/Volumes/1TBDrive/WorkingSet/******/nuxt-static-web/test/index.test.ts:1
import test from '../node_modules/ava';
       ^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Module.m._compile (/Volumes/1TBDrive/WorkingSet/******/nuxt-static-web/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at require.extensions.<computed> (/Volumes/1TBDrive/WorkingSet/******/nuxt-static-web/node_modules/ts-node/src/index.ts:442:12)
    at Object.require.extensions.<computed> [as .ts] (/Volumes/1TBDrive/WorkingSet/******/nuxt-static-web/node_modules/ava-ts/lib/process-adapter.js:100:4)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Volumes/1TBDrive/WorkingSet/******/nuxt-static-web/node_modules/ava-ts/lib/test-worker.js:65:1)

Config

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": [
      "esnext",
      "esnext.asynciterable",
      "dom"
    ],
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": false,
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ],
      "~/components/*": [
        "./components/*"
      ]
    },
    "types": [
      "@types/node",
      "@nuxt/vue-app"
    ]
  }
}

package.json

{
  "dependencies": {
    ...
    "ava": "^1.4.1",
    "ts-node": "^8.1.0",
  },
  "ava": {
    "compileEnhancements": false,
    "extensions": [
      "ts"
    ],
    "require": [
      "ts-node/register"
    ]
  }
}

Environment

Tell us which operating system you are using, as well as which versions of Node.js, npm, and AVA. Run the following to get it quickly:

Node.js v12.2.0
darwin 18.5.0
ava v1.4.1
npm v6.9.0

Most helpful comment

AVA does not support ES modules, you鈥檒l need to add the esm package like explained in the docs.

Where exactly? I was reading readme.md and it seems to dive right into the example, no mention of this prerequisite. Also not trace of this prerequisite in the sample package.json given:

{
    "name": "awesome-package",
    "scripts": {
        "test": "ava"
    },
    "devDependencies": {
        "ava": "^1.0.0"
    }
}

I was just following instructions from readme.md

import test from 'ava';

test('foo', t => {
    t.pass();
});

All 7 comments

"target": "esnext",

You've configured TypeScript to emit ESM syntax, which Node.js does not yet support. If possible you should change the target when running tests, but I'm not sure how that'd work with ts-node. You could also try the esm package but again I'm not sure how that interacts with ts-node.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

Possible solution for anyone who's looking:

  1. Drop "module": "es2015" from your tsconfig.json
  2. Use tsc --module es2015 whenever you call tsc _to build_ (as opposed to _to test_)

I am having the same issue, and I don't use Typescript, or any compiler at all.

node -v
v10.15.2

AVA does not support ES modules, you鈥檒l need to add the esm package like explained in the docs.

AVA does not support ES modules, you鈥檒l need to add the esm package like explained in the docs.

Where exactly? I was reading readme.md and it seems to dive right into the example, no mention of this prerequisite. Also not trace of this prerequisite in the sample package.json given:

{
    "name": "awesome-package",
    "scripts": {
        "test": "ava"
    },
    "devDependencies": {
        "ava": "^1.0.0"
    }
}

I was just following instructions from readme.md

import test from 'ava';

test('foo', t => {
    t.pass();
});

I googled it sweat_smile

Thank you very much!

I understand now, the issue was that I was trying to do node test.js or node test.mjs thinking that eh, it's gotta be the same than doing `npm bin`/ava or ava. It's not.

Was this page helpful?
0 / 5 - 0 ratings