Ram_T
Ram_T

Reputation: 8484

How to run Jest test cases with Vanilla JS?

I am trying to run test cases using jest with vanilla javascript.

login.test.js

import { validateEmail } from '../assets/js/common';

describe('validate email', () => {
    it('should pass with valid email', () => {
        const result = validateEmail('[email protected]');
        expect(result).toBe(true);    
    });
    it('should fail with invalid email', () => {
        const result = validateEmail('exampletest.com');
        expect(result).toBe(false);    
    });
})

common.js

export const validateEmail = (email) => {
    return email && EMAIL.test(email);
}

When I run npm run test, it is throwing

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){

import { validateEmail } from '../assets/js/common';

           ^^^^^^

SyntaxError: Unexpected token import

I followed many threads on GitHub but no use. They are saying about React, react-native, and also Babel config etc. But I don't have any .babelrc in my project. It's just html, css, pug, js and webpack.

Below is my project directory structure.

enter image description here

Upvotes: 2

Views: 3728

Answers (1)

Herman Starikov
Herman Starikov

Reputation: 2764

Es6 modules require babel. To set up babel with jest follow https://jestjs.io/docs/en/getting-started.html#using-babel, create the .babelrc as per the guide, and remove all react related parts since you don't use react here.

Upvotes: 2

Related Questions