Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import nock in TypeScript #1589

Closed
borekb opened this issue Jun 17, 2019 · 7 comments
Closed

How to import nock in TypeScript #1589

borekb opened this issue Jun 17, 2019 · 7 comments

Comments

@borekb
Copy link

borekb commented Jun 17, 2019

Just a note for anyone googling this (I had a hard time...):

This syntax is used in DefinitelyTyped tests:

import nock = require('nock');

However, it doesn't work with Babel (in our case, Jest uses Babel under the covers):

SyntaxError: example.ts: `import =` is not supported by @babel/plugin-transform-typescript
    Please consider using `import <moduleName> from '<moduleName>';` alongside Typescript's --allowSyntheticDefaultImports option.

This is the recommended syntax:

import nock from 'nock';

But that yielded another error when starting the app via ts-node:

example.ts:7
const mockApi = nock('https://example.com');
                    ^
TypeError: nock_1.default is not a function

It started working when I added "esModuleInterop": true to tsconfig.json.


Summary:

  • Use import nock from 'nock'
  • Add "esModuleInterop": true to tsconfig.json
@borekb
Copy link
Author

borekb commented Jun 17, 2019

(Sorry if this is not the best place to post it but I intuitively searched this repo for "TypeScript" and googled as well, without much luck. Feel free to close.)

@mastermatt
Copy link
Member

There could be discussion around whether we want to support export default too, to help with this issue. As a, primarily, TS dev these days I can appreciate this pain point.

index.js could be updated to have this

const scope = require('./lib/scope')
module.exports = scope
module.exports.default = scope

@borekb
Copy link
Author

borekb commented Jun 17, 2019

Would be a welcome change for sure 😄

@mastermatt
Copy link
Member

... and to extend on @borekb summary:

"esModuleInterop": true should be the go to for any TS app especially for an new app. If, however, you have an existing codebase where turning on esModuleInterop would be a pain, the alternative is to import using: import * as nock from "nock"; to get the same results.

@borekb
Copy link
Author

borekb commented Jun 18, 2019

the alternative is to import using: import * as nock from "nock"; to get the same results.

That's the first thing I usually try but it doesn't work for me:

TypeError: nock is not a function

Again, this is in the context of Babel (Jest tests). It works via ts-node.

@paulmelnikow
Copy link
Member

index.js could be updated to have this

const scope = require('./lib/scope')
module.exports = scope
module.exports.default = scope

Hmm, this seems a bit hacky solution for this problem which is an ecosystem issue.

Is it possible there is a problem with the types that is contributing to the difficulty with import nock from "nock"?

@borekb
Copy link
Author

borekb commented Jun 18, 2019

import nock from "nock" is importing a default export, which nock doesn't provide.

It's true that I'm fighting with this because of how Babel + TypeScript play together but it's a significant enough stack & the change for Nock would be small so I think it would be worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants