chaijs / chai

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
https://chaijs.github.io
MIT License
8.15k stars 698 forks source link

Migrating to Chai 5.0 in browser #1599

Closed schrotie closed 10 months ago

schrotie commented 10 months ago

This took me some time to figure out, a documentation update might help.

TLDR: You need to change

<script src="../node_modules/chai/chai.js"></script>

to

<script src="../node_modules/chai/chai.js" type="module"></script>

If you are using imperative Chai API calls like chai.should() you must use it in your own ESMs like this:

    import {should} from '../node_modules/chai/chai.js';
    should(); // was: chai.should()

The rest is documenting my journey to get it working again (I thought I couldn't fix this and wanted to file a bug for that):

Chai stopped working in the browser.

Old setup:

<script src="../node_modules/chai/chai.js"></script>

This now fails with

Uncaught SyntaxError: Unexpected token 'export' (at chai.js:3719:1)

Making it a module:

<script src="../node_modules/chai/chai.js" type="module"></script>

Fails with

Uncaught ReferenceError: chai is not defined

because it does no longer define the global "chai" (im using chai.should(), otherwise this might work)

Next try:

import {chai} from '../node_modules/chai/chai.js';
->
Uncaught SyntaxError: The requested module '../node_modules/chai/chai.js' does not provide an export named 'chai'

Aaaah, got it:

    import {should} from '../node_modules/chai/chai.js';

    should(); // was: chai.should()