diff --git a/.travis.yml b/.travis.yml index 7964c50da..2303d5e71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ jobs: name: 'Linting' script: npm run lint - name: 'Coverage' - script: npm run coverage && npm run coveralls + script: npm run coverage-coveralls - stage: release if: branch = master script: skip diff --git a/package.json b/package.json index 76de9805e..ad656b50a 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,11 @@ "browser": "dist/liquid.js", "scripts": { "lint": "eslint . --ext .ts --ext .js", - "unit": "mocha -r ts-node/register -r tsconfig-paths/register test/unit/**.ts", - "e2e": "npm run build && mocha -r ts-node/register -r tsconfig-paths/register test/e2e/**/*.ts", + "unit": "mocha test/unit/*.ts test/unit/**/*.ts", + "e2e": "npm run build && mocha test/e2e/**/*.ts test/e2e/*.ts", "test": "npm run unit && npm run e2e", - "coverage": "nyc --reporter=html npm run unit", - "coveralls": "nyc report --reporter=text-lcov | coveralls", + "coverage-html": "nyc npm run unit && nyc report --reporter=html", + "coverage-coveralls": "nyc npm run unit && nyc report --reporter=text-lcov | coveralls", "build": "rollup -c && ls -lh dist", "version": "npm run build && git add -A dist" }, @@ -68,6 +68,7 @@ "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", @@ -82,5 +83,8 @@ "@semantic-release/npm", "@semantic-release/git" ] + }, + "nyc": { + "extension": [ ".ts" ] } } diff --git a/src/parser/template.ts b/src/parser/template.ts index 360aec844..ef0d4ff7b 100644 --- a/src/parser/template.ts +++ b/src/parser/template.ts @@ -1,10 +1,12 @@ import * as _ from '../util/underscore' import * as path from 'path' import { anySeries } from '../util/promise' -import * as fs from 'fs' +import { stat, readFile } from 'fs' -const statFileAsync = <(filepath: string) => Promise>_.promisify(fs.stat) -const readFileAsync = <(filepath: string, encoding: string) => Promise>_.promisify(fs.readFile) +export const fs = { + stat: _.promisify(stat) as ((filepath: string) => Promise), + readFile: _.promisify(readFile) as ((filepath: string, encoding: string) => Promise) +} export async function resolve (filepath, root, options) { if (!path.extname(filepath)) { @@ -15,7 +17,7 @@ export async function resolve (filepath, root, options) { const paths = root.map(root => path.resolve(root, filepath)) return anySeries(paths, async path => { try { - await statFileAsync(path) + await fs.stat(path) return path } catch (e) { e.message = `${e.code}: Failed to lookup ${filepath} in: ${root}` @@ -25,5 +27,5 @@ export async function resolve (filepath, root, options) { } export async function read (filepath): Promise { - return readFileAsync(filepath, 'utf8') + return fs.readFile(filepath, 'utf8') } diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 000000000..bdc416ac0 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,2 @@ +--require ts-node/register +--require tsconfig-paths/register diff --git a/test/unit/options/trimming.ts b/test/unit/options/trimming.ts index 899aced70..63666c4be 100644 --- a/test/unit/options/trimming.ts +++ b/test/unit/options/trimming.ts @@ -12,7 +12,7 @@ describe('LiquidOptions#trimming', function () { }) it('should respect trim_tag_right', async function () { const engine = new Liquid({ trim_tag_right: true }) - const html = engine.parseAndRender('\t{%if true%}foo{%endif%} \n') + const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n') return expect(html).to.equal('\tfoo') }) it('should not trim value', async function () { diff --git a/test/unit/template.ts b/test/unit/template.ts index 146549a07..e894116ab 100644 --- a/test/unit/template.ts +++ b/test/unit/template.ts @@ -1,13 +1,28 @@ import { resolve } from '../../src/parser/template' -import * as mock from 'mock-fs' -import { expect } from 'chai' import * as path from 'path' +import { fs } from 'src/parser/template' +import { expect, use } from 'chai' +import * as chaiAsPromised from 'chai-as-promised' + +use(chaiAsPromised) describe('template', function () { + let readFile, stat before(function () { - mock({ - '/foo/bar.html': 'bar' - }) + 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 }) describe('#resolve()', function () { it('should resolve based on root', async function () { diff --git a/test/unit/util/error.ts b/test/unit/util/error.ts index fc065475e..96fdb2a11 100644 --- a/test/unit/util/error.ts +++ b/test/unit/util/error.ts @@ -47,7 +47,8 @@ describe('error', function () { it('should contain stack in err.stack', async function () { const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected expect(err.message).to.contain('illegal tag syntax') - expect(err.stack).to.contain('at Object.parse') + console.log(err.stack) + expect(err.stack).to.contain('at Liquid.parse') }) describe('captureStackTrace compatibility', function () { const captureStackTrace = Error.captureStackTrace diff --git a/test/unit/util/promise.ts b/test/unit/util/promise.ts index 1c551a99f..8747abb69 100644 --- a/test/unit/util/promise.ts +++ b/test/unit/util/promise.ts @@ -52,9 +52,9 @@ describe('util/promise', function () { .then(() => expect(spy).to.not.have.been.called) }) }) - describe('.mapSeries()', function () { + describe('.mapSeries()', async function () { it('should resolve when all resolved', async function () { - const result = P.mapSeries( + const result = await P.mapSeries( ['first', 'second', 'third'], item => Promise.resolve(item) )