sindresorhus / slugify

Slugify a string
MIT License
2.56k stars 81 forks source link

Cannot use Slugify inside Jest tests #64

Closed petersg83 closed 2 years ago

petersg83 commented 2 years ago

Slugify seems natively incompatible with jest. I get the following error when I run jest test where slugify is used in one of the test:

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/runner/slug/node_modules/@sindresorhus/slugify/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import escapeStringRegexp from 'escape-string-regexp';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | 'use strict'
      2 |
    > 3 | const slugify = require('@sindresorhus/slugify');
        |                 ^
      4 |
      5 | describe('slugify', () => {
      6 |   test('test 1', () => {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (slug.test.js:3:17)

My files:

// slug.test.js
'use strict'

const slugify = require('@sindresorhus/slugify');

describe('slugify', () => {
  test('test 1', () => {
    expect(slugify('démocratie')).toBe('democratie');
  });
});
// package.json
{
  "name": "slug",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@sindresorhus/slugify": "^2.1.0"
  },
  "dependencies": {
    "jest": "^27.4.7"
  }
}

Here is a repo that reproduces the error when you run jest or yarn test: https://replit.com/@PierreNoel/slug#slug.test.js

petersg83 commented 2 years ago

I just read that https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c, so I'm closing the issue since it is intended

krazyjakee commented 2 years ago

But how did you fix it?

I followed the wall of text you linked and found this page: https://github.com/facebook/jest/blob/64de4d7361367fd711a231d25c37f3be89564264/docs/ECMAScriptModules.md but none of the solutions actually solve the issue.