I have E2E Cypress tests and they all pass. However this one gives me a jest/no-standalone-expect ESLint error:
Then(/^I should see a headline "(.+)"/, title => {
cy.get('#Request').then((interception: unknown) => {
const interceptedRequest = interception;
cy.get(stuff[title]).then(heading => {
expect(heading).to.match(new RegExp(interceptedRequest.thing));
})
});
});
I have similar uses of expect in other tests without issues. How do I fix this one and what's causing the issue?
This is what eventually solved my issue - extracting the expect into a helper function.
Then(/^I should see a headline "(.+)"/, title => {
const matchHelper = (text, test) => {
expect(text).to.match(test)
}
cy.get('#Request').then((interception: unknown) => {
const interceptedRequest = interception;
cy.get(stuff[title]).then(heading => {
matchHelper(heading, new RegExp(interceptedRequest.thing));
})
});
});
It's one of the suggestions from the official docs, but still doesn't make much sense to me.
Related
Here is the code. I'm getting error TypeError: setSystemTime is not available when not using modern timers when I run the test. I have "#jest/fake-timers": "^27.4.2" in my package.json since I thought there could be a conflict on the package in some dependencies, but issue remains
beforeAll(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(new Date());
});
afterAll(() => {
jest.useRealTimers();
});
Any idea how to resolve this?
As mentioned in this issue, it can be solved by checking the version of jest and it's related packages. For example, I had jest on version 26.6.0 and babel-jest and ts-jest on 27.x. Setting them to 26.x solved the problem.
Facing the same issue here, this patch from #stereodenis is working (copying it here):
let dateNowSpy: jest.SpyInstance;
const today = new Date('2000-01-01').getTime();
describe('...', () => {
beforeEach(() => {
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => today);
});
afterEach(() => {
dateNowSpy.mockRestore();
});
test('...', () => {
/* Date.now() -> 2000-01-01 */
});
I am overwriting jsdom localstorage API like this in jest, so as to test my try catch code.
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
How do reset its implementation ?
I am presently doing this. Is there a cleaner way to do this ?
const setImplementation = Storage.prototype.setItem;
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
expect(() => {
localStorageHelper.setData(key, value);
}).not.toThrow();
Storage.prototype.setItem = setImplementation;
done();
Object methods should never be mocked by assigning, exactly because they cannot be restored this way. Restoring them manually is reinventing the wheel because this can be handled by Jest:
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error("error");
});
A mock like this one should be used with jest.restoreAllMocks() or restoreMock configuration option to be consistently restored without a risk to affect other tests.
I am new with JEST and have received the above warning. I want to know which is the alternative since is being deprecated.
Here is the test that I am making:
it('is instantiated', () => {
expect(wrapper.isVueInstance()).toBeTruthy();
});
Here's how to rigorously check VueInstance
https://github.com/vuejs/vue-test-utils/blob/2d6b49780c7e1d663b877ddf5d6492ed7b510379/packages/test-utils/src/wrapper.js#L400
it('is instantiated', () => {
expect(wrapper.vm).toBeTruthy();
});
I have checked https://vue-test-utils.vuejs.org/api/wrapper/#isvisible and they say:
Assert Wrapper is Vue instance.
So the final thing would be:
it('is instantiated', () => {
expect(wrapper).toBeTruthy();
});
The right answer should be the following:
it('is instantiated', () => {
expect(wrapper.exists()).toBeTruthy();
});
From test/specs/wrapper/find.spec.js in vue-test-utils repository,
you can see that when wrapper doesnt exists they assert Wrapper object with exists().
it('returns empty Wrapper with error if no nodes are found', () => {
const wrapper = mountingMethod(Component)
const selector = 'pre'
const error = wrapper.find(selector)
expect(error.exists()).toEqual(false)
expect(error.selector).toEqual(selector)
})
I am having problems loading filenames into jest.each asynchronously.
My code:
let files: string[][]
function getFilesWorking() {
files = [["test1"], ["test2"]]
}
async function getFilesAsync() {
files = await Promise.resolve([["test1"], ["test2"]])
}
beforeAll(() => {
console.log("before")
})
describe.only("Name of the group", () => {
getFilesAsync()
test.each(files)("runs", f => {})
})
beforeAll is executed before each test but NOT before initialization of test.each, so I end up with undefined.
How can I load files before using test.each?
You can pass an async callback to beforeAll and await getFilesAsync within it
beforeAll(async () => {
await getFilesAsync();
})
As of Jest 28.1.3 and prior, this is not possible. There is an open issue documenting this behavior.
The best thing you can do for now is put your tests in a regular it() test and do a deep value comparison:
it('tests an array of cases', async () => {
const data = await getSomeAsyncData()
const expectedData = [ ... ]
expect(data).toEqual(expectedData)
})
You can use beforeEach to set up code that will run prior to tests for any given scope, https://jestjs.io/docs/setup-teardown:
beforeEach(() => {
console.log('before every test');
});
describe.only(('Name of the group') => {
beforeEach(() => {
console.log('before tests in this describe block');
})
})
Jest is only going to run the tests in your describe.only block. If you want to use beforeEach in other blocks and run those tests as well, change describe.only to describe.
(Edit: I know this is a year late, I'm just trying to look for a similar problem/solution set and thought I could answer this.)
I am writing an automated test with Nightmare.js and typescript. The first objective of this test is to test I've landed at the correct page.
I have done some research, but have not found anything useful to me.
Here is my code:
import * as Nightmare from "nightmare";
describe("Login Page", function() { this.timeout("30s");
let nightmare = null; beforeEach(() => {
nightmare = new Nightmare({ show: true }); });
describe("give correct details", () => {
it("should log-in, check for current page url", done => {
nightmare
.goto(www.example.com/log-in)
.wait(5000)
.type("input[type='email']", "username")
.type("input[type='password']", "password")
.click(".submit")
.wait(3000)
SOMETHING THAT SHOULD CHECK URL HERE
.end()
.then(result => {
done();
})
.catch(done);
});
});
});
Where it says "SOMETHING THAT SHOULD CHECK URL HERE" I would like something to put the current url into a variable for later reference then console.log it. All answers are appreciated
Many ways to do it. I'll be using the simplest solution for this.
let SOMEGLOBALVAR; // so we can access it later
// Codes in between ...
.click(".submit")
.wait(3000)
.url() // <-- use the built in url
.end()
.then(url => {
SOMEGLOBALVAR = url; // set it to a global variable if you want
done();
})
You can change it however you want.
Note: Global vars are considered bad practice. The above snippet is just for illustrating the .url function.