Jest – Mock a constant property from a module for a specific test

File that exports a constant value that I want to mock: // utils/deviceTypeUtils file import DeviceInfo from ‘react-native-device-info’; export const isTablet = DeviceInfo.isTablet(); In my test file, I use this code to mock the constant isTablet: // file: deviceTypeUtils.spec const DeviceTypeUtilsMock = jest.requireMock(‘../utils/deviceTypeUtils’); jest.mock(‘../utils/deviceTypeUtils’, () => ({ isTablet: false, })); describe(‘mock const example’, () => … Read more

What is the difference between `main` and `module` vs `exports` in package.json?

Generally speaking, the “exports” field superseded the “module” field. “module” itself has never been an official standard but it became so widespread that at some point it was a de facto standard. Note that the “module” field is still being used by TypeScript if tsconfig’s moduleResolution is set to node; see microsoft/TypeScript#50794 for more info. … Read more

Named export ‘Types’ not found. The requested module ‘mongoose’ is a CommonJS module, which may not support all module.exports as named exports

TL;DR All you have to do is remove the curly braces from the Types. That should work. Just like this: import Types from ‘mongoose’ But the name does not matter. Explanation import Types from ‘mongoose’ works because we are importing the package’s default export (this is why the name we use does not matter). However, … Read more

Class constructor cannot be invoked without ‘new’ – typescript with commonjs

TypeScript transpiles a class to its ES5 counterpart, but this way it’s necessary that entire class hierarchy is transpiled to ES5. In case parent class is untranspiled (native class or imported ES6 class, including the ones that were transpiled with Babel), this won’t work, because TypeScript relies on var instance = Parent.call(this, …) || this … Read more

module.exports “Module is not defined”

RequireJS cannot load CommonJS modules as-is. However, there is a minimal modification you can make to them to load them. You have to wrap them in a define call like this: define(function (require, exports, module) { module.exports = { Combobox: require(‘./combobox’), Option: require(‘./option’) }; }); If you have a bunch of modules you need to … Read more

How to include commonjs module in ES6 module node app?

Working with CommonJS modules is pretty straight forward. You can only do default exports from CommonJS modules. import packageMain from ‘commonjs-package’; // Works import { method } from ‘commonjs-package’; // Errors This means that all commonjs exports will live on the packageMain object, and you need to dot in to the packageMain object to pickup … Read more

what is the difference between import and const and which is preferred in commonjs

What is the preferred method and what is the difference between using const and import? In 2016 it makes sense to stick with the import since that’s the part of the standard. There is no technical reason to prefer import over require though: everything that can be done using require can be done with import … Read more

How to correctly use ES6 “export default” with CommonJS “require”?

To use export default with Babel, you can do 1 of the following: require(“myStuff”).default npm install babel-plugin-add-module-exports –save-dev Or 3: //myStuff.js var thingToExport = {}; Object.defineProperty(exports, “__esModule”, { value: true }); exports[“default”] = thingToExport;