Skip to content

Migrating to Chai 5.0 in browser #1599

Description

@schrotie

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions