UNPKG

synctos

Version:

The Syncmaker. A tool to build comprehensive sync functions for Couchbase Sync Gateway.

62 lines (46 loc) 1.95 kB
const { expect } = require('chai'); const simpleMock = require('../../lib/simple-mock/index'); const mockRequire = require('mock-require'); describe('Stubbed environment maker', () => { let environmentMaker, fsMock, vmMock; beforeEach(() => { // Mock out the "require" calls in the module under test fsMock = { readFileSync: simpleMock.stub() }; mockRequire('fs', fsMock); vmMock = { runInThisContext: simpleMock.stub() }; mockRequire('vm', vmMock); environmentMaker = mockRequire.reRequire('./stubbed-environment-maker'); }); afterEach(() => { // Restore "require" calls to their original behaviour after each test case mockRequire.stopAll(); }); it('creates a environment from the input with a filename for stack traces', () => { verifyParse('my-sync-func-\\`1\\`', 'my-filename'); }); it('creates a environment from the input but without a filename', () => { verifyParse('my-sync-func-\\`2\\`'); }); function verifyParse(fileContents, filename) { const templateFile = 'my-template-file-path'; const macroName = '$my-template-macro$'; const envTemplateFileContents = `template: ${macroName}`; fsMock.readFileSync.returnWith(envTemplateFileContents); const expectedEnvString = envTemplateFileContents.replace(macroName, () => fileContents); const expectedResult = { bar: 'foo' }; vmMock.runInThisContext.returnWith(expectedResult); const result = environmentMaker.create(templateFile, macroName, fileContents, filename); expect(result).to.eql(expectedResult); expect(fsMock.readFileSync.callCount).to.equal(1); expect(fsMock.readFileSync.calls[0].args).to.eql([ templateFile, 'utf8' ]); expect(vmMock.runInThisContext.callCount).to.equal(1); expect(vmMock.runInThisContext.calls[0].args).to.eql([ `(${expectedEnvString});`, { filename: filename, displayErrors: true } ]); } });