Skip to content

Instantly share code, notes, and snippets.

@epreston
Created April 28, 2023 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epreston/19d4bd07e46ffcfba24e75aadf7435e1 to your computer and use it in GitHub Desktop.
Save epreston/19d4bd07e46ffcfba24e75aadf7435e1 to your computer and use it in GitHub Desktop.
vitest - generic test file format example
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
// import { itemsToMock } from '../src/testDependency.js';
// ---------------------------------------------------------
// vi.mock('../src/testDependency.js');
// ---------------------------------------------------------
// import { itemBeingTested } from '../src/testTarget.js';
// ---------------------------------------------------------
const props = {
foo: vi.fn(),
bar: {
baz: true,
},
};
// ---------------------------------------------------------
describe('Object', () => {
let instance;
beforeAll(() => {
instance = Object.assign({}, props);
});
// beforeEach(() => {
// instance = Object.assign({}, props);
// });
it('should expose an object', () => {
expect(instance).toBeDefined();
expect(instance).toBeInstanceOf(Object);
});
it('should have the expected properties', () => {
expect(instance).toHaveProperty('bar');
});
it('should have a method foo()', () => {
// instance.foo();
expect(instance.foo).toBeDefined();
instance.foo();
expect(instance.foo).toHaveBeenCalled();
});
it('should load and configure the object correctly', () => {
expect(instance).toMatchSnapshot();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment