'Karma fails to load test file: "Failed to fetch dynamically imported module"
I'm trying to test my LitElement components with Karma but importing PDF.JS is causing the test file not to be loaded.
Example of the LitElement component file
import * as pdfjsLib from 'pdfjs-dist/build/pdf.js';
import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry.js';
import * as pdfjsSandbox from 'pdfjs-dist/build/pdf.sandbox.js';
import * as pdfjsViewer from 'pdfjs-dist/web/pdf_viewer.js';
import pdfjsViewerCss from 'pdfjs-dist/web/pdf_viewer.css';
export class UiDocReviewContext extends ScopedElementsMixin(LitElement) {
static get properties() {
return {
....
Example of the Karma test file
import '../ui-doc-review-context.js';
describe('feat-upload', async () => {
beforeEach(async () => {
await fixture(html`<ui-doc-review-context
.data=${{}}
.pageNum=${1}
.box=${{}}
></ui-doc-review-context>`);
});
})
The issue
Karma gives the following error:
Chrome Headless 100.0.4896.127 (Mac OS 10.15.7) ERROR: 'Error loading test file: /packages/ui-doc-review-context/test/ui-doc-review-context.test.js'
Chrome Headless 100.0.4896.127 (Mac OS 10.15.7) ERROR: 'TypeError: Failed to fetch dynamically imported module: http://localhost:9876/base/packages/ui-doc-review-context/test/ui-doc-review-context.test.js?c2a0b72b2b28b1f3a57be6918056fed53cfcd90c'
In the console logs I get this:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/css". Strict MIME type checking is enforced for module scripts per HTML spec.
context.js:265 Error loading test file: /packages/ui-doc-review-context/test/ui-doc-review-context.test.js
context.js:265 TypeError: Failed to fetch dynamically imported module: http://localhost:9876/base/packages/ui-doc-review-context/test/ui-doc-review-context.test.js?c2a0b72b2b28b1f3a57be6918056fed53cfcd90c
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
