Koushik1408
Koushik1408

Reputation: 51

TypeError: sha256_1.default is not a function

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!!

Upvotes: 5

Views: 5702

Answers (2)

Al Fahad
Al Fahad

Reputation: 2568

This syntax worked for me

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

Upvotes: 8

Matt McCutchen
Matt McCutchen

Reputation: 30929

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.

Upvotes: 2

Related Questions