vendredi 12 juin 2020

Getting ParseError: Identifier is expected on importing svelte files for testing with jest

My Jest config details are

jest.config.js

module.exports = {
    transform: {
        '^.+\\.svelte$': 'svelte-jester',
        '^.+\\.js$': 'babel-jest',
    },
    moduleFileExtensions: ['js', 'svelte'],
}

babel.config.js

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current',
                },
            },
        ],
    ],
}

package.json

  .
  .
  "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "babel-jest": "^26.0.1",
    "jest": "^26.0.1",
    "svelte-jester": "^1.0.6",
    "@testing-library/svelte": "^3.0.0"
  },
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack",
    "dev": "webpack-dev-server --content-base public",
    "test": "jest src",
    "test:watch": "npm run test -- --watch"
  },
  .
  .

I created src/test folder where my test.spec.js is as follows

import {fireEvent, render} from '@testing-library/svelte';

import App from '../App.svelte';

describe('test', () => {
    test('Just a mock test', async () => {
        const myMock = jest.fn();
        console.log(myMock());

        myMock.mockReturnValueOnce(10).mockReturnValueOnce('x').mockReturnValue(true);

        console.log(myMock(), myMock(), myMock(), myMock());
    });
});

Plz note that this I used a jest mock function just for testing purpose but whenever I import a svelte file as in this case App.svelte I get an error as below

FAIL src/test/test.spec.js ● Test suite failed to run

ParseError: Identifier is expected

Aucun commentaire:

Enregistrer un commentaire