Skip to content

Migrating to Chai 5.0 in browser #1599

Closed

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

Activity

  1. locked and limited conversation to collaborators on Feb 2, 2024
  2. converted this issue into a discussion #1600 on Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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