karma-runner / karma-mocha

A Karma plugin. Adapter for Mocha testing framework.
MIT License
379 stars 95 forks source link

Await all `script type="module"` before mocha.run #244

Closed loynoir closed 2 years ago

loynoir commented 2 years ago

Tested solved https://github.com/karma-runner/karma-mocha/issues/243

loynoir commented 2 years ago
$ git clone https://github.com/loynoir/reproduce-karmaMocha-237.mjs .
$ pnpm install
$ pnpm test
  CJS
    ✔ bar
  fastESM
    ✔ bar

SUMMARY:
✔ 2 tests completed
$ pnpm remove karma-mocha && pnpm install https://github.com/loynoir/karma-mocha && pnpm test
  CJS
    ✔ bar
  fastESM
    ✔ bar
  slowESM
    ✔ bar

SUMMARY:
✔ 3 tests completed
google-cla[bot] commented 2 years ago

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

loynoir commented 2 years ago

Changes

  1. Await all script type="module" before mocha.run. If await failed, add a unit test, and throw error within.
  2. Remove lib/adapter.js from gitignore, so can use npm install <repo>.
  3. mocha.requireHooks now supported, load thru {type:"import", value:"/base/my/path/to/mochaHooks.mjs"}
  4. Add unit test to verify mocha.requireHooks.beforeAll works.
  5. Using mocha.requireHooks.afterAll to verify mocha.run run after all slow ESM modules.
  6. devDependencies updated, to support requireHooks and type = 'module'

```txt Uncaught TypeError: this[opt] is not a function ``` Update mocha from 3.5.3 to 9.1.3, to support rootHooks ```txt Uncaught SyntaxError: Unexpected token 'export ``` Update karma from 1.7.1 to 6.3.6, to support files.type = 'module' ```txt WARN [karma-server]: Passing raw CLI options to `new Server(config, done)` is deprecated. Use `parseConfig(c onfigFilePath, cliOptions, {promiseConfig: true, throwErrors: true})` to prepare a processed `Config` instance and pass that as the `config` argument instead. ``` Update grunt-karma from 2.0.0 to 4.0.2, to get rid of warning.

  1. README.md updated for requireHooks

Fixme/todo:

  1. import(), reported SyntaxError by low version eslint
  2. Remove importSync() and importSyncFallback() when mocha accept serializable .requireHooks, or accept import() as .requireHooks input.