@nx/enforce-module-boundaries ESLint Rule #9551
-
|
I am running into an Nx-specific issue where code following the below pattern, established in the import { expect, vi } from 'vitest'
import { answer } from './example.js'
vi.mock(import('./example.js'), async (importOriginal) => {
const originalModule = await importOriginal()
return {
answer: vi.fn(originalModule.answer),
variable: 'mock',
}
})
expect(answer()).toBe(42)
expect(answer).toHaveBeenCalled()
expect(answer).toHaveReturned(42)The error message from the rule is "Static imports of lazy-loaded libraries are forbidden." It would be triggered in the above example at the I can get around this by adding a bunch of Has anyone else run into this and/or solved it some other way? I tried editing my |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
The - vi.mock(import('./example.js'), async (importOriginal) => {
- const originalModule = await importOriginal()
+ vi.mock('./example.js', async (importOriginal) => {
+ const originalModule: typeof import('./example.js') = await importOriginal()
return {
answer: vi.fn(originalModule.answer),
variable: 'mock',
}
})Though personally I would just disable that ESLint rule from any test files. |
Beta Was this translation helpful? Give feedback.
-
|
As a follow-up, ignoring test files for that rule doesn't seem to work. Apparently, it still sees both the lazy and static imports and triggers the error from the code files (not the test files). Switching to the string form did work, though. |
Beta Was this translation helpful? Give feedback.
The
vi.mock(import('./example.js')is just syntax sugar for typescript. You can write the same code withoutimport()as:Though personally I would just disable that ESLint rule from any test files.