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

TypeError: before is not a function

Open iamlothian opened this issue 6 years ago • 6 comments
trafficstars

vscode: Version 1.33.1 (1.33.1) Name: Mocha Test Explorer Id: hbenl.vscode-mocha-test-adapter Description: Run your Mocha tests in the Sidebar of Visual Studio Code Version: 1.9.0 Publisher: Holger Benl VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter OS: macOS High Sierra 10.13.6

If I have the following test code

'use strict'
/* global describe it */
/* eslint no-undef: 0 */

const { assert } = require('chai')
const { before, after, describe, it } = require('mocha')

describe("api suppliers", () => {

    before("setup", () => {
        console.log("setup")
    })

    after("teardown", () => {
        console.log("teardown")
    })

    it("has an integration test", () => {
        assert(true, "passing")
    })

})

mochaExplorer fails to load and all the test disappear from the test explorer (is this expected behaviour that one file breaks the plugin for all tests) with the following error in the output.

TypeError: before is not a function
    at Suite.describe (/Users/matthew.lothian/.../integration-tests/example.test.js:11:5)
    at Object.create (/Users/matthew.lothian/.vscode/extensions/hbenl.vscode-mocha-test-adapter-1.9.0/node_modules/mocha/lib/interfaces/common.js:140:19)
    at context.describe.context.context (/Users/matthew.lothian/.vscode/extensions/hbenl.vscode-mocha-test-adapter-1.9.0/node_modules/mocha/lib/interfaces/bdd.js:42:27)
    at /Users/matthew.lothian/.vscode/extensions/hbenl.vscode-mocha-test-adapter-1.9.0/out/worker/patchMocha.js:44:37
    at Object.<anonymous> (/Users/matthew.lothian/.../integration-tests/example.test.js:9:1)
    at Object.<anonymous> (/Users/matthew.lothian/.../integration-tests/example.test.js:24:3)
    at Module._compile (internal/modules/cjs/loader.js:711:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:722:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:559:12)

iamlothian avatar May 16 '19 00:05 iamlothian

Seems to be the includes that are causing the issue. if I just require('mocha') only it works

const { assert } = require('chai')
require('mocha')

This doesn't seem to be consistent though as if I try this on another test they still complain about before and after if I don't include either of this in the test then it works.

iamlothian avatar May 16 '19 00:05 iamlothian

Ok. I found it, somewhere in the code a module is redefining and exporting before and after. This has no effect on running mocha itself which correctly required and defines package in order, but the explorer seems to handle this differently and fails to distinguish between the function definitions.

iamlothian avatar May 16 '19 01:05 iamlothian

This issue effects for every imports from 'mocha' package. I got similar message: "mocha_1.describe is not a function". it looks like effecting to all imports in mocha package.

gchovanyecz avatar Aug 23 '19 15:08 gchovanyecz

@gchovanyecz why do you try to require the describe function anyway? These functions are defined as globals when you run your tests with mocha, so there's usually no need to require them.

hbenl avatar Aug 23 '19 18:08 hbenl

I suspect this is related to vscode-mocha-test-adapter using its own bundled copy of mocha, related to the issue hit in #11.

@gchovanyecz @iamlothian if you set the mochaExplorer.mochaPath option, does the problem go away?

cspotcode avatar Nov 27 '19 02:11 cspotcode

@gchovanyecz why do you try to require the describe function anyway? These functions are defined as globals when you run your tests with mocha, so there's usually no need to require them.

I just removed those requires and it works good 👍

casareafer2908 avatar Dec 08 '22 08:12 casareafer2908