diff --git a/package.json b/package.json index ad656b50a..a7950875a 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,6 @@ "express": "^4.16.4", "jsdom": "^13.2.0", "mocha": "^5.2.0", - "mock-fs": "^4.7.0", "nyc": "^13.1.0", "regenerator-runtime": "^0.12.1", "rollup": "^1.1.2", @@ -68,7 +67,6 @@ "semantic-release": "^15.13.3", "sinon": "^7.2.3", "sinon-chai": "^3.3.0", - "source-map-support": "^0.5.10", "supertest": "^3.4.2", "ts-node": "^8.0.2", "tsconfig-paths": "^3.8.0", diff --git a/test/e2e/express.ts b/test/e2e/express.ts index 1ae026d1c..fb38ec579 100644 --- a/test/e2e/express.ts +++ b/test/e2e/express.ts @@ -1,8 +1,8 @@ import { expect } from 'chai' import * as request from 'supertest' import * as express from 'express' -import * as mock from 'mock-fs' -import Liquid from '../../dist/liquid.common.js' +import { mock, restore } from 'test/stub/mockfs' +import Liquid from '../..' describe('express()', function () { var app, engine @@ -24,9 +24,7 @@ describe('express()', function () { file: req.params.file })) }) - after(function () { - mock.restore() - }) + after(restore) it('should render express views', function (done) { mock({ '/views/name.html': 'My name is {{name}}.' }) app.set('views', ['/views']) diff --git a/test/e2e/render-file.ts b/test/e2e/render-file.ts index c2eaec566..3c7da317b 100644 --- a/test/e2e/render-file.ts +++ b/test/e2e/render-file.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' -import * as mock from 'mock-fs' import Liquid from '../..' +import { mock, restore } from '../stub/mockfs' describe('#renderFile()', function () { var engine @@ -13,14 +13,10 @@ describe('#renderFile()', function () { '/root/files/bar': 'bar', '/root/files/foo.html': 'foo', '/root/files/name.html': 'My name is {{name}}.', - '/un-readable.html': mock.file({ - mode: '0000' - }) + '/un-readable.html': { mode: '0000' } }) }) - afterEach(function () { - mock.restore() - }) + afterEach(restore) it('should render file', async function () { const html = await engine.renderFile('/root/files/foo.html', {}) return expect(html).to.equal('foo') diff --git a/test/stub/mockfs.ts b/test/stub/mockfs.ts new file mode 100644 index 000000000..2ad63f01f --- /dev/null +++ b/test/stub/mockfs.ts @@ -0,0 +1,31 @@ +import { fs } from 'src/parser/template' +import { isString } from 'src/util/underscore' + +const readFile = fs.readFile +const stat = fs.stat + +type fileDescriptor = { mode: string, content: string } + +export function mock (files: { [path: string]: (string | fileDescriptor) }) { + for (const [key, val] of Object.entries(files)) { + files[key] = isString(val) + ? { mode: '33188', content: val as string } + : val + } + fs.readFile = async function (path) { + const file = files[path] as fileDescriptor + if (file === undefined) throw new Error('ENOENT') + if (file.mode === '000') throw new Error('EACCES') + return file.content + } + fs.stat = async function (path) { + const file = files[path] as fileDescriptor + if (file === undefined) throw new Error('ENOENT') + return file + } +} + +export function restore () { + fs.readFile = readFile + fs.stat = stat +} diff --git a/test/unit/liquid.ts b/test/unit/liquid.ts index 3bf00f677..a12014b57 100644 --- a/test/unit/liquid.ts +++ b/test/unit/liquid.ts @@ -1,6 +1,6 @@ import Liquid from '../../src/liquid' -import * as mock from 'mock-fs' import * as chai from 'chai' +import { mock, restore } from 'test/stub/mockfs' const expect = chai.expect @@ -36,9 +36,7 @@ describe('Liquid', function () { '/root/foo': 'foo' }) }) - after(function () { - mock.restore() - }) + after(restore) it('should render single template', function (done) { render.call({ root: '.' }, 'foo', null, (err, result) => { if (err) return done(err) diff --git a/test/unit/options/cache.ts b/test/unit/options/cache.ts index 7ac742cd2..f55e48703 100644 --- a/test/unit/options/cache.ts +++ b/test/unit/options/cache.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' -import * as mock from 'mock-fs' -import Liquid from '../../../src/liquid' +import Liquid from 'src/liquid' +import { mock, restore } from 'test/stub/mockfs' describe('LiquidOptions#cache', function () { let engine @@ -11,9 +11,7 @@ describe('LiquidOptions#cache', function () { }) mock({ '/root/files/foo.html': 'foo' }) }) - afterEach(function () { - mock.restore() - }) + afterEach(restore) it('should be disabled by default', function () { return engine.renderFile('files/foo') .then(x => expect(x).to.equal('foo')) diff --git a/test/unit/tags/include.ts b/test/unit/tags/include.ts index eeeff8d55..7a9613a69 100644 --- a/test/unit/tags/include.ts +++ b/test/unit/tags/include.ts @@ -1,6 +1,6 @@ import Liquid from 'src/liquid' import { expect } from 'chai' -import * as mock from 'mock-fs' +import { mock, restore } from 'test/stub/mockfs' describe('tags/include', function () { let liquid @@ -10,9 +10,7 @@ describe('tags/include', function () { extname: '.html' }) }) - afterEach(function () { - mock.restore() - }) + afterEach(restore) it('should support include', async function () { mock({ '/current.html': 'bar{% include "bar/foo.html" %}bar', diff --git a/test/unit/tags/layout.ts b/test/unit/tags/layout.ts index 1cfc2a957..53a579388 100644 --- a/test/unit/tags/layout.ts +++ b/test/unit/tags/layout.ts @@ -1,6 +1,6 @@ import Liquid from 'src/liquid' import { expect } from 'chai' -import * as mock from 'mock-fs' +import { mock, restore } from 'test/stub/mockfs' describe('tags/layout', function () { let liquid @@ -10,9 +10,7 @@ describe('tags/layout', function () { extname: '.html' }) }) - afterEach(function () { - mock.restore() - }) + afterEach(restore) it('should throw when block not closed', function () { mock({ diff --git a/test/unit/template.ts b/test/unit/template.ts index e894116ab..d23102e7b 100644 --- a/test/unit/template.ts +++ b/test/unit/template.ts @@ -1,29 +1,15 @@ import { resolve } from '../../src/parser/template' import * as path from 'path' -import { fs } from 'src/parser/template' import { expect, use } from 'chai' import * as chaiAsPromised from 'chai-as-promised' +import { mock, restore } from '../stub/mockfs' use(chaiAsPromised) describe('template', function () { - let readFile, stat - before(function () { - readFile = fs.readFile - stat = fs.stat - fs.readFile = async function (file) { - if (file === '/foo/bar.html') return 'bar' - throw new Error('NOENT') - } - fs.stat = async function (file) { - if (file === '/foo/bar.html') return { type: 'file' } - throw new Error('NOENT') - } - }) - after(function () { - fs.readFile = readFile - fs.stat = stat - }) + before(() => mock({ '/foo/bar.html': 'bar' })) + after(restore) + describe('#resolve()', function () { it('should resolve based on root', async function () { const filepath = await resolve('bar.html', '/foo', { root: [] }) diff --git a/test/unit/util/error.ts b/test/unit/util/error.ts index 96fdb2a11..038c205ec 100644 --- a/test/unit/util/error.ts +++ b/test/unit/util/error.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import Liquid from 'src/liquid' -import * as mock from 'mock-fs' import * as path from 'path' +import { mock, restore } from 'test/stub/mockfs' let engine = new Liquid() const strictEngine = new Liquid({ @@ -10,9 +10,7 @@ const strictEngine = new Liquid({ }) describe('error', function () { - afterEach(function () { - mock.restore() - }) + afterEach(restore) describe('TokenizationError', function () { it('should throw TokenizationError when tag illegal', async function () { @@ -66,7 +64,7 @@ describe('error', function () { '/foo.html': html }) const err = await expect(engine.renderFile('/foo.html')).be.rejected - mock.restore() + restore() expect(err.name).to.equal('TokenizationError') expect(err.file).to.equal(path.resolve('/foo.html')) }) @@ -207,7 +205,7 @@ describe('error', function () { '/foo.html': html }) const err = await expect(engine.renderFile('/foo.html')).be.rejected - mock.restore() + restore() console.log(err, err.name) expect(err.name).to.equal('RenderError') expect(err.file).to.equal(path.resolve('/foo.html')) @@ -299,7 +297,7 @@ describe('error', function () { '/foo.html': html }) const err = await expect(engine.renderFile('/foo.html')).be.rejected - mock.restore() + restore() expect(err.name).to.equal('ParseError') expect(err.file).to.equal(path.resolve('/foo.html')) }) diff --git a/tsconfig.json b/tsconfig.json index b03a3a721..c8317eb48 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,8 @@ "baseUrl": ".", "paths": { "template": ["src/parser/template"], - "src/*": ["src/*"] + "src/*": ["src/*"], + "test/*": ["test/*"] } }, "include": [ "src", "test" ],