@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
86 lines (69 loc) • 2.64 kB
JavaScript
;
jest.mock('node-sass', () => ({
render: jest.fn()
}));
jest.mock('fs');
const originalExit = process.exit;
const originalArgv = process.argv;
describe('runCssCompile', () => {
beforeEach(() => {
process.exit = jest.fn();
});
afterEach(() => {
process.exit = originalExit;
process.argv = originalArgv;
jest.resetModules();
jest.restoreAllMocks();
});
it('should return css file path on success', async () => {
const {
render
} = require('node-sass');
const fs = require('fs');
render.mockImplementationOnce((config, callback) => callback(null, {}));
fs.writeFile.mockImplementationOnce((filename, data, callback) => callback(null));
const runCssCompile = require('./runCssCompile');
const result = await runCssCompile({
file: '/blah/root/styles.scss'
});
expect(result).toBe('/blah/root/styles.css');
});
it('should throw custom error if something went wrong but not error is thrown by the compiler', async () => {
const {
render
} = require('node-sass');
const fs = require('fs');
render.mockImplementationOnce((config, callback) => callback(null, null));
fs.writeFile.mockImplementationOnce((filename, data, callback) => callback(null));
const runCssCompile = require('./runCssCompile');
await expect(runCssCompile({
file: '/blah/root/styles.scss'
})).rejects.toThrow(/Compiler returned null result/i);
});
it('should throw if a compiler error occurs', async () => {
const {
render
} = require('node-sass');
const errorMessage = 'Oh shit, something went wrong with the compiler'; // eslint-disable-next-line promise/prefer-await-to-callbacks
render.mockImplementationOnce((config, callback) => // eslint-disable-next-line promise/prefer-await-to-callbacks
callback(new Error(errorMessage)));
const runCssCompile = require('./runCssCompile');
await expect(runCssCompile({
file: '/blah/root/styles.scss'
})).rejects.toThrow(errorMessage);
});
it('should throw if a write error occurs', async () => {
const {
render
} = require('node-sass');
const fs = require('fs');
render.mockImplementationOnce((config, callback) => callback(null, {}));
const errorMessage = 'Oh shit, something went wrong on file write';
fs.writeFile.mockImplementation((filename, data, callback) => callback(new Error(errorMessage)));
const runCssCompile = require('./runCssCompile');
expect.assertions(1);
await expect(runCssCompile({
file: '/blah/root/styles.scss'
})).rejects.toThrow(errorMessage);
});
});