3

I am getting below error while using jest to test. I'm using a non es6 npm package here. The issue happens when I do not want to do any wildcard * imports. I wanted to do explicit imports.

//code

import sha256 from "crypto-js/sha256";

export const GetHashToken = (value) => {
 return sha256(value);
};

//test

import { GetHashToken, GetUtcTimeStamp } from 
"../../utils/ScheduleRepairUtility";

describe("Get hash token utility", () => {
  it("Should return sha-256 hex digit string for given value", () => {
    const paramValue = "78436783465sears121212";   
    expect(GetHashToken(paramValue))
     .toBe("b87663752c7f5bf0967f74fd3523dece543
      f447e96d9b1d478c7336781e1828b");
  });
});

Getting error:

●  Get hash token utility › Should return sha-256 hex digit string for 
   given value

TypeError: sha256_1.default is not a function

I'm using react-typescript for my project. Any help is appreciated.

Thanks!!

1
  • Just FYI: Everything works fine when I do something like: import * as sha256 from "crypto-js" Oct 3, 2018 at 0:37

2 Answers 2

3

This syntax worked for me

import * as sha256 from 'crypto-js/sha256';
2

I assume the crypto-js/sha256 module exports the sha256 function via a CommonJS-style export assignment (module.exports = sha256). To import it, you either need to use TypeScript's special import assignment syntax:

import sha256 = require("crypto-js/sha256");

or enable the esModuleInterop compiler option that provides interoperability between default imports and export assignments.

1
  • Thanks Matt. I got it to work using createHash from @types/node instead of crypto-js/sha256. Oct 4, 2018 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.