Skip to content

Commit da00c2a

Browse files
author
Alexander Yukal
committed
refactor(tests): update tests
use native nodejs test and assert modules instead mocha and chai
1 parent 2a82575 commit da00c2a

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

test/test.loader.js

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
'use strict';
22

3-
const Path = require('path');
4-
const { assert, expect } = require('chai');
3+
// Use nodeJS native test modules, see:
4+
//
5+
// https://nodejs.org/api/test.html
6+
// https://nodejs.org/api/assert.html
7+
//
8+
// node@16: node --test ./test
9+
// node@18: node --test --experimental-test-coverage ./test
10+
// node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info
11+
12+
const path = require('node:path');
13+
const assert = require('node:assert');
14+
const { describe, it } = require('node:test');
15+
516
const JsonLoader = require('../lib/gulp-json-loader');
617

718
const {
@@ -18,27 +29,28 @@ const {
1829

1930
const Imports = require('./data');
2031

21-
const getAbsolutePath = (subdir) => Path.join(process.cwd(), subdir);
32+
const getAbsolutePath = (subdir) => path.join(process.cwd(), subdir);
2233
const getPugFile = (filename) => ({
2334
path: getAbsolutePath(`./src/data/pages/${filename}.pug`)
2435
});
2536

2637
describe('Gulp Json Loader', () => {
27-
2838
describe('factory()', () => {
2939
it('init with default settings', () => {
3040
const testMode = true;
3141
const settings = undefined;
3242

3343
const { context } = JsonLoader(settings, testMode);
3444

35-
expect(context).property('pathHtml', getAbsolutePath('./src/html'));
36-
expect(context).property('pathData', getAbsolutePath('./src/data'));
37-
expect(context).property('sourcePath', 'src');
38-
expect(context).property('dataEntry', '$');
39-
expect(context).property('locales', 'ru-UA');
40-
expect(context).property('cachedData').eql({});
41-
expect(context).property('report', true);
45+
assert.deepEqual(context, {
46+
pathHtml: getAbsolutePath('./src/html'),
47+
pathData: getAbsolutePath('./src/data'),
48+
sourcePath: 'src',
49+
dataEntry: '$',
50+
locales: 'uk-UA',
51+
cachedData: {},
52+
report: true,
53+
});
4254
});
4355

4456
it('init with a specific Data entry', () => {
@@ -48,8 +60,7 @@ describe('Gulp Json Loader', () => {
4860
};
4961

5062
const { context } = JsonLoader(settings, testMode);
51-
52-
expect(context).property('dataEntry', '$Data');
63+
assert.equal(context.dataEntry, '$Data');
5364
});
5465

5566
it('init with a specific html & data paths', () => {
@@ -61,9 +72,9 @@ describe('Gulp Json Loader', () => {
6172

6273
const { context } = JsonLoader(settings, testMode);
6374

64-
expect(context).property('pathHtml', getAbsolutePath('./src/pug'));
65-
expect(context).property('pathData', getAbsolutePath('./src/json'));
66-
expect(context).property('dataEntry', 'data');
75+
assert.equal(context.pathHtml, getAbsolutePath('./src/pug'));
76+
assert.equal(context.pathData, getAbsolutePath('./src/json'));
77+
assert.equal(context.dataEntry, 'data');
6778
});
6879

6980
it('init with a specific source path', () => {
@@ -74,10 +85,10 @@ describe('Gulp Json Loader', () => {
7485

7586
const { context } = JsonLoader(settings, testMode);
7687

77-
expect(context).property('pathHtml', getAbsolutePath('./source/html'));
78-
expect(context).property('pathData', getAbsolutePath('./source/data'));
79-
expect(context).property('sourcePath', 'source');
80-
expect(context).property('dataEntry', 'data');
88+
assert.equal(context.pathHtml, getAbsolutePath('./source/html'));
89+
assert.equal(context.pathData, getAbsolutePath('./source/data'));
90+
assert.equal(context.sourcePath, 'source');
91+
assert.equal(context.dataEntry, 'data');
8192
});
8293

8394
it('convert root sign into an internal path', () => {
@@ -88,8 +99,8 @@ describe('Gulp Json Loader', () => {
8899

89100
const { context } = JsonLoader(settings, testMode);
90101

91-
expect(context).property('pathHtml', getAbsolutePath('./html'));
92-
expect(context).property('pathData', getAbsolutePath('./data'));
102+
assert.equal(context.pathHtml, getAbsolutePath('./html'));
103+
assert.equal(context.pathData, getAbsolutePath('./data'));
93104
});
94105

95106
it('throw error on referencing an external path', () => {
@@ -102,7 +113,7 @@ describe('Gulp Json Loader', () => {
102113
JsonLoader(settings, testMode);
103114
assert.fail('should throw an error');
104115
} catch (error) {
105-
expect(error.message).equal(constants.ERR_EXTERNAL_PATH);
116+
assert.equal(error.message, constants.ERR_EXTERNAL_PATH);
106117
}
107118
});
108119
});
@@ -125,8 +136,8 @@ describe('Gulp Json Loader', () => {
125136
const cacheKeyData = 'src/data/pages/about.json';
126137

127138
// Check out loaded data
128-
expect(pocket).property('filename', 'about');
129-
expect(pocket).property('$').eql({
139+
assert.equal(pocket.filename, 'about');
140+
assert.deepEqual(pocket.$, {
130141
name: 'About Us',
131142
href: 'about-us.html',
132143
visible: true,
@@ -136,8 +147,8 @@ describe('Gulp Json Loader', () => {
136147
});
137148

138149
// Check out cached data
139-
expect(ctx.cachedData).property(cacheKeyData).eql(pocket);
140-
expect(ctx.cachedData).property(cacheKeyImports).eql(pocket.$.imports.genres);
150+
assert.deepEqual(ctx.cachedData[cacheKeyData], pocket);
151+
assert.deepEqual(ctx.cachedData[cacheKeyImports], pocket.$.imports.genres);
141152
});
142153
});
143154

@@ -153,8 +164,8 @@ describe('Gulp Json Loader', () => {
153164
const importedData = await loadImportsAsync(context, imports, cachedData);
154165
const cacheKey = 'src/data/imports/genres.json';
155166

156-
expect(importedData).property('genres').eql(Imports.genres);
157-
expect(cachedData).property(cacheKey).eql(Imports.genres);
167+
assert.deepEqual(importedData.genres, Imports.genres);
168+
assert.deepEqual(cachedData[cacheKey], Imports.genres);
158169
});
159170

160171
it('import from cache', async () => {
@@ -173,7 +184,7 @@ describe('Gulp Json Loader', () => {
173184

174185
const importedData = await loadImportsAsync(context, imports, cachedData);
175186

176-
expect(importedData).eql({
187+
assert.deepEqual(importedData, {
177188
genres: cachedData[cacheKey]
178189
});
179190
});
@@ -184,7 +195,7 @@ describe('Gulp Json Loader', () => {
184195
const imports = [];
185196

186197
const importedData = await loadImportsAsync(context, imports, cachedData);
187-
expect(importedData).eql({});
198+
assert.deepEqual(importedData, {});
188199
});
189200

190201
it('throw error on non-array passed', async () => {
@@ -196,7 +207,7 @@ describe('Gulp Json Loader', () => {
196207
await loadImportsAsync(context, imports, cachedData);
197208
assert.fail('should throw an error');
198209
} catch (err) {
199-
expect(err.message).equal('Imports should be an Array');
210+
assert.equal(err.message, 'Imports should be an Array');
200211
}
201212
});
202213
});
@@ -219,13 +230,13 @@ describe('Gulp Json Loader', () => {
219230
};
220231

221232
await loadImportsRecursively(options, (error, pocket) => {
222-
expect(error).equal(null);
233+
assert.equal(error, null);
223234

224-
expect(pocket).property('menu').eql(Imports.menu);
225-
expect(pocket).property('genres').eql(Imports.genres);
235+
assert.deepEqual(pocket.menu, Imports.menu);
236+
assert.deepEqual(pocket.genres, Imports.genres);
226237

227-
expect(options.cachedData).property(cacheKeyMenu);
228-
expect(options.cachedData).property(cacheKeyGenres);
238+
assert.equal(options.cachedData.hasOwnProperty(cacheKeyMenu), true);
239+
assert.equal(options.cachedData.hasOwnProperty(cacheKeyGenres), true);
229240
});
230241
});
231242

@@ -241,8 +252,8 @@ describe('Gulp Json Loader', () => {
241252
};
242253

243254
await loadImportsRecursively(options, (error, pocket) => {
244-
expect(error.message).equal(constants.ERR_EXTERNAL_PATH);
245-
expect(pocket).equal(null);
255+
assert.equal(error.message, constants.ERR_EXTERNAL_PATH);
256+
assert.equal(pocket, null);
246257
});
247258
});
248259
});

0 commit comments

Comments
 (0)