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

Isolation is not working #47

Closed
acsvln opened this issue Oct 14, 2018 · 6 comments · Fixed by #49
Closed

Isolation is not working #47

acsvln opened this issue Oct 14, 2018 · 6 comments · Fixed by #49
Assignees

Comments

@acsvln
Copy link

acsvln commented Oct 14, 2018

I have no idea why isolation is not working in my project.
I have two files:
Test file:

// process.env.NODE_ENV = 'test';

const HttpStatusCode = require('http-statuscode');
const chai = require('chai');
const chaiHttp = require('chai-http');
const rewiremock = require('rewiremock').default

const should = chai.should();

chai.use(chaiHttp);

const db_mock = require('./mock/db_mock');

rewiremock('../lib/db')
    .with(db_mock);

rewiremock.enable();
rewiremock.isolation();
rewiremock.passBy(/node_modules/);

const app = require('../src/app');
// const route = require('../src/route/index');

rewiremock.withoutIsolation();
rewiremock.disable();

App file:

const express = require('express');
const path = require('path');

const app = express();

const db = require("./lib/db");
const test = require("./route/test");
const api = require("./route/api");
const www = require("./route");

// TODO: use here express.static when we would have lots of files
app.get('/static/item_db.css', function (req, res) {
  res.sendFile(path.resolve(__dirname, "./public/item_db.css"));
});

app.use('/', www);
app.use('/api', api);

db.refresh();

test()

module.exports = app;

As we can see test route is NOT mocked, but isolation does not care about this.

@theKashey theKashey self-assigned this Oct 14, 2018
@theKashey
Copy link
Owner

Should work. Is it actually mocking properly?

@acsvln
Copy link
Author

acsvln commented Oct 14, 2018

yes, mock in const db_mock = require('./mock/db_mock'); works well

@theKashey
Copy link
Owner

Try

 rewiremock.isolation({
      noAutoPassBy: true
    });
  • meanwhile I am trying to remember why this feature is not documented

theKashey added a commit that referenced this issue Oct 16, 2018
@theKashey theKashey mentioned this issue Oct 16, 2018
@theKashey
Copy link
Owner

theKashey commented Oct 16, 2018

rewiremock.passBy(/node_modules/) was breaking the stuff, as long mocha was a parent of a test, and was while-listed, thus allowing all their "children" to pass.
After PR is being merged, your example should be fine, without any code changes.

@theKashey
Copy link
Owner

https://github.com/theKashey/rewiremock/tree/3.9.0

@acsvln
Copy link
Author

acsvln commented Oct 21, 2018

Everything worked, thanks

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

Successfully merging a pull request may close this issue.

2 participants