Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It doesn't run my setup/global file as mocha does #240

Open
orekav opened this issue May 28, 2020 · 3 comments
Open

It doesn't run my setup/global file as mocha does #240

orekav opened this issue May 28, 2020 · 3 comments

Comments

@orekav
Copy link

orekav commented May 28, 2020

When I run "npm test" --> mocha './test/**/*.spec.js', it runs perfectly.
But when I run it with mocha sidebar it doesn't pay attention to my "setup.spec.js"

To avoid this behavior with mocha-sidebar I have to add at the beginning of each test file this: require("../setup.spec");

//  setup.spec.js
const chaiAsPromised = require("chai-as-promised");
const chai = require("chai");

chai.use(chaiAsPromised);
chai.should();

const sequelize = require("../src/models/index");

before(async () => {
	await sequelize.sync({ force: true });
});

after(async () => {
	await sequelize.close();
});

I have this error:
TypeError: Cannot read property 'eventually' of undefined

If I add that file as required I have the message: "before is not defined"


{
	"folders": [
		{
			"path": "."
		}
	],
	"settings": {
		"mocha.env": {
			"NODE_ENV": "test",
			"DATABASE_URL": "mariadb://root:mypass@172.17.0.2:3306/node_test_travis",
			"DATABASE_USE_SSL": "false"
		},
		"mocha.files.glob": "./test/**/*.spec.js",
		"mocha.parallelTests": 1,
		// "mocha.coverage": null
		"mocha.requires": ["chai-as-promised", "chai", "mocha", "./test/setup.spec.js"],
		"mocha.runTestsOnSave": "true",
		"mocha.options": {
			
		}
	}
}

Does anyone know how can I run setup.spec.js before everything?
require("./setup.spec"); in each test file is not an option.

@madfrag
Copy link

madfrag commented Jun 5, 2020

I have the same issue, need some help

@orekav
Copy link
Author

orekav commented Jun 5, 2020

I will start the migration to jest because also with Sinon + Decache I am facing issues with my app.js file when I tried to mock a Middleware (Google Captcha). It works perfectly, except for the Sequelize connection that remains OPEN because of the "decache". That provokes the test run never quits.

@kozak-codes
Copy link

Tried for a bit to figure this out; --require is loaded before mocha is initialized so if you have a global hook in there it will run but won't work. I ended up just doing a replace-all for import 'mocha'; -> import 'mocha';\nimport 'tests/globals'; and that worked pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants