Skip to content

Dynamic import with catch block fails in test #326

@otakustay

Description

@otakustay

Describe the bug

An await import statement with a catch block runs well in NodeJS but fails in vitest

node --eval "require('./index').run().then(console.log)"
false # console output

Reproduction

// index.js
exports.run = async () => {
    try {
        await import('./404.js');
    }
    catch (ex) {
        if (ex.code === 'MODULE_NOT_FOUND' || ex.code === 'ERR_MODULE_NOT_FOUND') {
            return false;
        }
        throw ex;
    }
};
// index.test.js
import {test, expect} from 'vitest';
import {run} from './index';

test('run', async () => {
    const value = await run();
    expect(value).toBe(false);
});

Running vitest produces errors:

RUN  /Users/otakustay/Downloads/vi

 ❯ index.test.js (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  index.test.js [ index.test.js ]
Error: Failed to resolve import "./404.js" from "index.js". Does the file exist?
 ❯ formatError node_modules/vite/dist/node/chunks/dep-4a9cff06.js:42631:46
 ❯ TransformContext.error node_modules/vite/dist/node/chunks/dep-4a9cff06.js:42627:19
 ❯ normalizeUrl node_modules/vite/dist/node/chunks/dep-4a9cff06.js:81376:26
 ❯ async TransformContext.transform node_modules/vite/dist/node/chunks/dep-4a9cff06.js:81516:57
 ❯ async Object.transform node_modules/vite/dist/node/chunks/dep-4a9cff06.js:42847:30
 ❯ async doTransform node_modules/vite/dist/node/chunks/dep-4a9cff06.js:57929:29

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

Test Files  1 failed (1)
     Tests  no tests
      Time  355ms (in thread 0ms, Infinity%)

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 8.58 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - /var/folders/hm/3h2qtx2x2jddg130mktcrztm0000gn/T/fnm_multishells/79464_1640586000170/bin/node
    Yarn: 1.22.15 - /var/folders/hm/3h2qtx2x2jddg130mktcrztm0000gn/T/fnm_multishells/79464_1640586000170/bin/yarn
    npm: 8.3.0 - /var/folders/hm/3h2qtx2x2jddg130mktcrztm0000gn/T/fnm_multishells/79464_1640586000170/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Safari: 15.2
  npmPackages:
    vite: ^2.7.7 => 2.7.7
    vitest: ^0.0.115 => 0.0.115

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions