sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.45k stars 185 forks source link

Cannot find module 'p-queue' from 'test/q.test.ts' #135

Closed moltar closed 3 years ago

moltar commented 3 years ago

I am not sure where the problem is, but I cannot seem to get the latest major upgrade (pure ESM) to work in Jest with TypeScript.

Simplest failure mode:

import PQueue from 'p-queue'

describe('test', () => {
  it('test', async () => {
    expect.assertions(1)

    expect(PQueue).toBeDefined()
  })
})
 FAIL  test/q.test.ts
  ● Test suite failed to run

    Cannot find module 'p-queue' from 'test/q.test.ts'

    > 1 | import PQueue from 'p-queue'
        | ^
      2 |
      3 | describe('test', () => {
      4 |   it('test', async () => {

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
      at Object.<anonymous> (test/q.test.ts:1:1)

Jest config:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testPathIgnorePatterns: ['<rootDir>/node_modules/'],
  roots: ['<rootDir>/src', '<rootDir>/test'],
}

Any ideas?

Thanks!

jumoog commented 3 years ago

I have the same Problem.. im using ts-node

sindresorhus commented 3 years ago

See https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

nguyentoanit commented 3 years ago

@sindresorhus: I have the same problem too.

Tried to apply your guide but it doesn't work fine. 🤔 I did a minimal reproduction in this repo.

moltar commented 3 years ago

Same here. I've tried all possible solution permutations and nothing helps.

ianbarker commented 3 years ago

I'm also running into this issue with jest. Strangely it seems to have no problem with p-map

Narixius commented 3 years ago

Hi guys,

I had the same issue and I tried to fix it by mocking this library.

I created a file in __mocks__/p-queue.ts

// __mocks__/p-queue.ts

export default class PQueue {
  constructor() {
    return this;
  }
  add(fn) {
    return fn();
  }
  pause() {}
  clear() {}
}

In my small project, I just used add, pause and clear methods. But you can add any other methods that you are usng in your project to make it work.