K
K
kyzinatra2020-07-06 07:38:02
JavaScript
kyzinatra, 2020-07-06 07:38:02

How to use Jest and webpack imports?

Z bundle files with webpack and use its imports which it then bundles

export function sum(a, b) {
  return a + b;
}


The question is how do I test functions using such imports.
import { sum } from "./index";

test("Sum of numbers", () => {
  expect(sum(1, 3)).toBe(4);
  expect(sum(6, 7)).toBe(15);
  expect(sum(12, 12)).toBe(24);
  expect(sum(24, 24)).toBe(48);
})


throws an error on startup

SyntaxError: Cannot use import statement outside a module


How to solve this problem.

PS: Babel is used to compile js
{
        test: /\.js(x)?$/,
        loader: "babel-loader",
        options: {
          presets: ["@babel/preset-env", "@babel/preset-react"],
          plugins: [
            "@babel/plugin-proposal-object-rest-spread",
            "@babel/plugin-proposal-class-properties",
            "@babel/plugin-proposal-private-methods",
          ],
        },
        exclude: /node_modules/,
      },

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question