Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"program": "${workspaceRoot}/packages/build/node_modules/mocha/bin/_mocha",
"runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"],
"cwd": "${workspaceRoot}",
"autoAttachChildProcesses": true,
"args": [
"--config",
"${workspaceRoot}/packages/build/config/.mocharc.json",
Expand All @@ -26,6 +27,7 @@
"program": "${workspaceRoot}/bin/mocha-current-file",
"runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"],
"cwd": "${workspaceRoot}",
"autoAttachChildProcesses": true,
"args": [
"--config",
"${workspaceRoot}/packages/build/config/.mocharc.json",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"build:site": "./bin/build-docs-site.sh",
"docs:prepare": "./docs/bin/build-preview-site.sh",
"docs:start": "cd docs/_preview && bundle exec jekyll serve --no-w --i",
"mocha": "node packages/build/bin/run-mocha --lang en_US.UTF-8 --timeout 5000 \"packages/*/dist/__tests__/**/*.js\" \"extensions/*/dist/__tests__/**/*.js\" \"examples/*/dist/__tests__/**/*.js\" \"packages/cli/test/**/*.js\" \"packages/build/test/*/*.js\"",
"mocha": "node packages/build/bin/run-mocha --lang en_US.UTF-8 --timeout 15000 --parallel \"packages/*/dist/__tests__/**/*.js\" \"extensions/*/dist/__tests__/**/*.js\" \"examples/*/dist/__tests__/**/*.js\" \"packages/cli/test/**/*.js\" \"packages/build/test/*/*.js\"",
"posttest": "npm run lint"
},
"config": {
Expand Down
1 change: 0 additions & 1 deletion packages/build/bin/run-mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function run(argv, options) {
!utils.isOptionSet(
mochaOpts,
'--config', // mocha 6.x
'--opts', // legacy
'--package', // mocha 6.x
'--no-config', // mocha 6.x
) && !utils.mochaConfiguredForProject();
Expand Down
1 change: 0 additions & 1 deletion packages/build/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ function mochaConfiguredForProject() {
'.mocharc.json',
'.mocharc.yaml',
'.mocharc.yml',
'test/mocha.opts',
];
return configFiles.some(f => {
const configFile = path.join(getPackageDir(), f);
Expand Down
2 changes: 1 addition & 1 deletion packages/build/config/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": "source-map-support/register",
"require": ["source-map-support/register"],
"recursive": true,
"exit": true,
"reporter": "dot"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// IMPORTANT
// This snapshot file is auto-generated, but designed for humans.
// It should be checked into source control and tracked carefully.
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests.
// Make sure to inspect the changes in the snapshots below.
// Do not ignore changes!

'use strict';

exports[`common matches snapshot 1`] = `
common result
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// IMPORTANT
// This snapshot file is auto-generated, but designed for humans.
// It should be checked into source control and tracked carefully.
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests.
// Make sure to inspect the changes in the snapshots below.
// Do not ignore changes!

'use strict';

exports[`test1 matches snapshot 1 1`] = `
test1 result
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// IMPORTANT
// This snapshot file is auto-generated, but designed for humans.
// It should be checked into source control and tracked carefully.
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests.
// Make sure to inspect the changes in the snapshots below.
// Do not ignore changes!

'use strict';

exports[`test2 matches snapshot 2 1`] = `
test2 result
`;
2 changes: 1 addition & 1 deletion packages/cli/test/integration/cli/cli.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const main = require('../../../lib/cli');
const {
expectToMatchSnapshot,
expectFileToMatchSnapshot,
} = require('../../snapshots');
} = require('../../snapshots')();

function getLog(buffer) {
buffer = buffer || [];
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/test/integration/dummy/common.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

exports.commonTest = function () {
describe('common', () => {
const {expectToMatchSnapshot} = require('../../snapshots')();

it('matches snapshot 1', function () {
expectToMatchSnapshot('common result');
});
});
};
16 changes: 16 additions & 0 deletions packages/cli/test/integration/dummy/test1.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const {expectToMatchSnapshot} = require('../../snapshots')();

describe('test1', () => {
it('matches snapshot 1', () => {
expectToMatchSnapshot('test1 result');
});
});

require('./common.integration').commonTest();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling commonTest outside of any describe block does not make sense to me. It creates duplicate entries in the list of test cases. In my experience with shared test suites, a shared test suite is typically called from inside a describe block, to scope each instance of the suite to the particular scenario being tested.

For example:

https://github.com/strongloop/loopback-next/blob/ae6427322451c914ae54f44dbb656981e7fbbb81/acceptance/repository-cloudant/src/__tests__/cloudant-default-repository.acceptance.ts#L13-L20

16 changes: 16 additions & 0 deletions packages/cli/test/integration/dummy/test2.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const {expectToMatchSnapshot} = require('../../snapshots')();

describe('test2', () => {
it('matches snapshot 2', () => {
expectToMatchSnapshot('test2 result');
});
});

require('./common.integration').commonTest();
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const props = {
};
const {expect} = require('@loopback/testlab');

const {assertFilesToMatchSnapshot} = require('../../snapshots');
const {assertFilesToMatchSnapshot} = require('../../snapshots')();

const tests = require('../lib/project-generator')(
generator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const tests = require('../lib/artifact-generator')(generator);
const baseTests = require('../lib/base-generator')(generator);
const testUtils = require('../../test-utils');

const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const generator = path.join(__dirname, '../../../generators/datasource');
const tests = require('../lib/artifact-generator')(generator);
const baseTests = require('../lib/base-generator')(generator);
const testUtils = require('../../test-utils');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/discover');
require('../lib/artifact-generator')(generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const path = require('path');
const generator = path.join(__dirname, '../../../generators/import-lb3-models');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();
const testUtils = require('../../test-utils');

const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const generator = path.join(__dirname, '../../../generators/interceptor');
const SANDBOX_FILES = require('../../fixtures/interceptor').SANDBOX_FILES;
const testUtils = require('../../test-utils');

const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();
const {remove} = require('fs-extra');

const generator = path.join(__dirname, '../../../generators/model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const path = require('path');
const {TestSandbox} = require('@loopback/testlab');
const {assertFilesToMatchSnapshot} = require('../../snapshots');
const {assertFilesToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/openapi');
const specPath = path.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const path = require('path');
const {TestSandbox} = require('@loopback/testlab');
const {assertFilesToMatchSnapshot} = require('../../snapshots');
const {assertFilesToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/openapi');
const specPath = path.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const path = require('path');
const {TestSandbox} = require('@loopback/testlab');
const {assertFilesToMatchSnapshot} = require('../../snapshots');
const {assertFilesToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/openapi');
const specPath = path.join(__dirname, '../../fixtures/openapi/3.0/uspto.yaml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const path = require('path');
const {TestSandbox} = require('@loopback/testlab');
const {assertFilesToMatchSnapshot} = require('../../snapshots');
const {assertFilesToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/openapi');
const specPath = path.join(__dirname, '../../fixtures/openapi/3.0/uspto.yaml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/relation');
const {SANDBOX_FILES, SourceEntries} = require('../../fixtures/relation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/relation');
const {SANDBOX_FILES, SourceEntries} = require('../../fixtures/relation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const {expect, TestSandbox} = require('@loopback/testlab');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

const generator = path.join(__dirname, '../../../generators/relation');
const {SANDBOX_FILES, SourceEntries} = require('../../fixtures/relation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TestSandbox = testlab.TestSandbox;
const generator = path.join(__dirname, '../../../generators/service');
const SANDBOX_FILES = require('../../fixtures/service').SANDBOX_FILES;
const testUtils = require('../../test-utils');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TestSandbox = testlab.TestSandbox;
const generator = path.join(__dirname, '../../../generators/rest-crud');
const SANDBOX_FILES = require('../../fixtures/rest-crud').SANDBOX_FILES;
const testUtils = require('../../test-utils');
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

// Test Sandbox
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const specPath = path.join(
__dirname,
'../../fixtures/openapi/2.0/petstore-expanded-swagger.json',
);
const {expectFileToMatchSnapshot} = require('../../snapshots');
const {expectFileToMatchSnapshot} = require('../../snapshots')();

const testlab = require('@loopback/testlab');
const TestSandbox = testlab.TestSandbox;
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/test/snapshot-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ move this file to a standalone package so that all Mocha users can use it.
const chalk = require('chalk');
const assert = require('assert');
const path = require('path');
const debug = require('debug')('test:snapshot-matcher');

module.exports = {
initializeSnapshots,
Expand All @@ -43,6 +44,22 @@ module.exports = {
* ```
*/
function initializeSnapshots(snapshotDir) {
if (debug.enabled) {
const stack = new Error().stack
.split(/\n/g)
// Remove the error message and the top stack frame pointing to ourselves
// and pick three frames (max), that should be enough to identify
// which test file called us.
.slice(2, 5)
.map(f => `\n${f}`)
.join();
debug(
'Initializing snapshot matcher, storing snapshots in %s%s',
snapshotDir,
stack,
);
}

let currentTest;
let snapshotErrors = false;

Expand Down
Loading