monounity / karma-typescript

Simplifying running unit tests with coverage for Typescript projects.
313 stars 109 forks source link

ReferenceError: exports is not defined #625

Open bnn16 opened 2 months ago

bnn16 commented 2 months ago

I seem to have this issue that I can't seem to fix. I updated my main tsconfig recently and for some reason my tests now fail. Full log can be found here : https://pastebin.com/fTHJZ77S ( I deleted majority of the repeating stuff from debug )

karma versions -

"karma": "^6.4.3",
    "karma-chrome-launcher": "^3.2.0",
    "karma-jasmine": "^5.1.0",
    "karma-jasmine-dom-matchers": "^0.1.2",
    "karma-jasmine-html-reporter": "^2.1.0",
    "karma-spec-reporter": "0.0.36",
    "karma-typescript": "^5.4",
    "karma-typescript-es6-transform": "^5.5.4",

my tsconfig is

{
    "compilerOptions": {
        "baseUrl": "./",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "target": "ES6",
        "module": "ESNext",
        "moduleResolution": "node",
        "noFallthroughCasesInSwitch": true,
        "noErrorTruncation": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitOverride": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "resolveJsonModule": true,
        "sourceMap": true,
        "jsx": "react-jsx",
        "lib": [
            "ESNext",
            "DOM"
        ],
        "typeRoots": [
            "./declarations",
            "./node_modules/@types"
        ],
        "types": [
            "jasmine",
            "jasmine_dom_matchers",
            "node",
            "resize-observer-browser"
        ],
        "paths": {
            "@foo/foo-lab": [
                "./dist/core/"
            ],
        }
    },
    "include": [
        "lib/**/*",
        "storybook/**/*",
        "declarations/**/*",
        "performance/**/*",
        "scripts/**/*",
        "wdio.conf.mts"
    ],
    "exclude": [
        "dist"
    ],
    "angularCompilerOptions": {
        "enableI18nLegacyMessageIdFormat": false,
        "strictInjectionParameters": true,
        "strictInputAccessModifiers": true,
        "strictTemplates": true
      }
}

My tsconfig.test.json

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "module": "CommonJS",
        "sourceMap": true,
        "noEmitOnError": true,
        "paths": {
            "worker-loader*": [
                "./lib/core/common/test/WebWorkerMock.ts"
            ],
            "@foo/foo-lab": [
                "./lib/core/"
            ]
        },
        "skipLibCheck": true,
    },
    "include": [
        "lib/**/*.ts",
        "declarations/**/*"
    ],
    "exclude": [
        "node_modules",
    ]
}

and finally my karma.conf.js

const isCoverageEnabled = () => process.argv.includes('--coverage');

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: [
            'jasmine',
            'karma-typescript',
            'jasmine-dom-matchers',
        ],
        files: [
            'lib/core/test.config.ts',
            'lib/core/**/*.ts',
            'lib/core/**/*.scss',
        ],
        client: {
            clearContext: false,
            jasmine: { failSpecWithNoExpectations: true },
        },
        plugins: [
            'karma-spec-reporter',
            'karma-jasmine',
            'karma-jasmine-dom-matchers',
            'karma-typescript',
            'karma-jasmine-html-reporter',
            'karma-chrome-launcher',
            '@alasdair/karma-scss-preprocessor',
        ],
        preprocessors: {
            'lib/core/**/*.ts': 'karma-typescript',
            'lib/core/**/*.scss': [ 'scss' ],
        },
        reporters: [ 'spec', 'karma-typescript' ],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: [ 'Chrome' ],
        singleRun: false,
        concurrency: Infinity,
        karmaTypescriptConfig: {
            tsconfig: './tsconfig.test.json',
            reports: {
                'html': {
                    'directory': 'coverage',
                    'subdirectory': () => '',
                },
                'lcovonly': {
                    'directory': 'coverage',
                    'subdirectory': () => '',
                },
            },
            bundlerOptions: {
                sourceMap: true,
                transforms: [
                    require('karma-typescript-es6-transform')(),
                ],
            },
            coverageOptions: {
                instrumentation: isCoverageEnabled(),
                exclude: [
                    /\.(d|spec|test)\.ts$/i,
                    /(\\|\/)index\.ts$/i,
                ],
                threshold: {
                    global: {
                        statements: 60,
                        branches: 60,
                        functions: 60,
                        lines: 60,
                    },
                },
            },
        },
    });
};

For whatever reason it doesn't work. I tried https://github.com/monounity/karma-typescript/issues/19 and https://github.com/monounity/karma-typescript/issues/111, I can't seem to find the issue. Any idea what might be causing it