vscode-jasmine-test-adapter icon indicating copy to clipboard operation
vscode-jasmine-test-adapter copied to clipboard

Worker: Caught error TypeError: Jasmine is not a constructor

Open taji opened this issue 2 years ago • 0 comments

I'm running a single unit test in firebase/functions with jasmine via the cli :

import {} from 'jasmine';
import { httpHelloWorldFunction } from './http.js';

describe('functions/core/functions/http', () => {
    it('send to be called with "hello from Firebase"', () => {

        const req = {  data: { foo: 'world' } };
        const res = {  send: (s) => {} };
        spyOn(res, 'send').and.callThrough();

        httpHelloWorldFunction(req, res);

        expect(res.send).toHaveBeenCalledWith("Hello from Firebase!");
    });
});

Target code is this :

import * as functions from "firebase-functions";

export function httpHelloWorldFunction(request, response) {
    response.send("Hello from Firebase!");
};

The test runs and passes without issue if I run it via the CLI. However I've encountered two issues with trying to run it via the Jasmine Test Explorer dashboard.

Issue 1: the Jasmine Log emits a JSON token error when trying to parse process.argv[5] (which is undefined), error here :

/home/todd/.vscode/extensions/hbenl.vscode-jasmine-test-adapter-1.8.2/out/worker/loadTests.js:12
    logEnabled = JSON.parse(process.argv[5]);
                      ^
SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (/home/todd/.vscode/extensions/hbenl.vscode-jasmine-test-adapter-1.8.2/out/worker/loadTests.js:12:23)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .js] (/home/todd/Dev/resell-it-1/functions/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)

Issue 2: I modified loadTests.ts to set logEnabled to true and then encountered this error in the Test Explorer Log:

/home/todd/.vscode/extensions/hbenl.vscode-jasmine-test-adapter-1.8.2/out/worker/loadTests.js:15
    const jasmine = new Jasmine({});
                    ^
TypeError: Jasmine is not a constructor
    at Object.<anonymous> (/home/todd/.vscode/extensions/hbenl.vscode-jasmine-test-adapter-1.8.2/out/worker/loadTests.js:15:21)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .js] (/home/todd/Dev/resell-it-1/functions/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)

Due to the file structure (all target and test code under the functions folder), I've had to configure my settings json as so:

{
    "git.confirmSync": false,
    "window.zoomLevel": 1,
    "jasmineExplorer.cwd": "functions/",
    "jasmineExplorer.debuggerConfig": "Debug Jasmine Tests",
    "jasmineExplorer.nodeArgv": ["-r", "ts-node/register", "--experimental-loader"],
    "jasmineExplorer.config": "jasmine.json",
    "jasmineExplorer.jasminePath": "functions/node_modules/jasmine",
    "testExplorer.codeLens": true,
    "testExplorer.gutterDecoration": true,
    "jasmineExplorer.logpanel": true,
}

And here are my launch.json and jasmine.json files:

   {
      "name": "Debug Jasmine Tests",
      "type": "pwa-node",
      "request": "attach",
      "port": 9229,
      "continueOnAttach": true,
      "autoAttachChildProcesses": false,
      "skipFiles": [
        "<node_internals>/**"
      ]
    }
{
  "spec_dir": "./src/core",
  "spec_files": [
    "**/*.spec.ts"
  ],
  "helpers": [
    "jasmine/*.ts"
  ]
}

And my tsconfig file:

{
  "compilerOptions": {
    "lib": [
      "ES2021"
    ],
    "module": "ES2020",
    "moduleResolution": "node",
    "target": "ES2021",
    "outDir": "lib",
    "sourceMap": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": [
    "src"
  ]
}

Any ideas? Thanks :-)

taji avatar Aug 25 '22 18:08 taji