Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch from proxyquire to @node-loaders/mock #20549

Merged
merged 1 commit into from
Dec 9, 2022
Merged
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: 1 addition & 1 deletion .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { pathToFileURL } = require('url');

const loaderPath = require.resolve('@node-loaders/esbuild/strict');
const loaderPath = require.resolve('@node-loaders/auto/strict');
const loaderUrl = pathToFileURL(loaderPath).href;

module.exports = {
Expand Down
90 changes: 36 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@
"yeoman-generator": "5.7.0"
},
"devDependencies": {
"@node-loaders/esbuild": "0.5.1",
"@node-loaders/auto": "0.6.0",
"@node-loaders/esbuild": "0.6.0",
"@node-loaders/mock": "0.7.0",
"@types/chai": "4.3.4",
"@types/mocha": "10.0.0",
"@types/sinon-chai": "3.2.9",
Expand All @@ -147,7 +149,6 @@
"jsdoc": "4.0.0",
"mocha": "10.1.0",
"mocha-expect-snapshot": "6.2.0",
"proxyquire": "2.1.3",
"rimraf": "3.0.2",
"sinon": "15.0.0",
"sinon-chai": "3.7.0",
Expand Down
4 changes: 2 additions & 2 deletions test/cli/import-jdl.spec.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { mockRequire } = require('@node-loaders/mock/require');
const path = require('path');
const proxyquire = require('proxyquire');
const fse = require('fs-extra');
const assert = require('yeoman-assert');
const expect = require('chai').expect;
Expand Down Expand Up @@ -76,7 +76,7 @@ const loadImportJdl = options => {
},
...options,
};
return proxyquire('../../cli/import-jdl.cjs', options);
return mockRequire('../../cli/import-jdl.cjs', options);
};

const defaultAddedOptions = {};
Expand Down
28 changes: 15 additions & 13 deletions test/cli/jdl.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('yeoman-assert');
const expect = require('chai').expect;
const https = require('https');
const fse = require('fs-extra');
const proxyquire = require('proxyquire').noCallThru().noPreserveCache();
const { mockRequire } = require('@node-loaders/mock/require');
const sinon = require('sinon');

const { testInTempDir, revertTempDir } = require('./utils/utils.cjs');
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('jdl command test', () => {
const env = { env: 'foo' };
const fork = { fork: 'foo' };
beforeEach(() => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jdl']], options, env, fork).then(jdlFiles => {
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jdl']], options, env, fork).then(jdlFiles => {
resolved = jdlFiles;
});
});
Expand All @@ -119,7 +119,7 @@ describe('jdl command test', () => {
const env = { env: 'foo' };
const fork = { fork: 'foo' };
beforeEach(() => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jdl', 'bar.jdl']], options, env, fork);
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jdl', 'bar.jdl']], options, env, fork);
});
it('should not call https.get', () => {
expect(https.get.callCount).to.be.equal(0);
Expand All @@ -139,7 +139,7 @@ describe('jdl command test', () => {
https.get.restore();
});
it('should return file not found', () => {
return proxyquire('../../cli/jdl.cjs', {})(
return mockRequire('../../cli/jdl.cjs', {})(
[['foo.jdl']],
{ bar: 'foo', skipSampleRepository: true },
{ env: 'foo' },
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('jdl command test', () => {
https.get.restore();
});
it('should call https.get', () => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })(
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })(
[['https://raw.githubusercontent.com/jhipster/jdl-samples/main/foo.jdl']],
{ bar: 'foo', skipSampleRepository: true },
{ env: 'foo' },
Expand All @@ -183,7 +183,7 @@ describe('jdl command test', () => {
});
});
it('should call importJdl', () => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })(
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })(
[['https://raw.githubusercontent.com/jhipster/jdl-samples/main/foo.jdl']],
{ bar: 'foo', skipSampleRepository: true },
{ env: 'foo' },
Expand Down Expand Up @@ -226,9 +226,11 @@ describe('jdl command test', () => {
const env = { env: 'foo' };
const fork = { fork: 'foo' };
beforeEach(() => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jh']], options, env, fork).then(jdlFiles => {
resolved = jdlFiles;
});
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo.jh']], options, env, fork).then(
jdlFiles => {
resolved = jdlFiles;
}
);
});
it('should pass to https.get with jdl-sample repository', () => {
expect(https.get.getCall(0).args[0]).to.be.equal(
Expand All @@ -254,7 +256,7 @@ describe('jdl command test', () => {

describe('when passing foo', () => {
beforeEach(() => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo']]);
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([['foo']]);
});
it('should append jdl extension and pass to https.get with jdl-sample repository', () => {
expect(https.get.getCall(0).args[0]).to.be.equal(
Expand All @@ -273,7 +275,7 @@ describe('jdl command test', () => {
describe('with a complete url', () => {
const url = 'https://raw.githubusercontent.com/jhipster/jdl-samples/main/bar.jdl';
beforeEach(() => {
return proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([[url]]);
return mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': importJdlStub })([[url]]);
});
it('should forward the url to get', () => {
expect(https.get.getCall(0).args[0]).to.be.equal(url);
Expand Down Expand Up @@ -306,14 +308,14 @@ describe('jdl command test', () => {
});

it('should not create the destination file', done => {
proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': () => {} })([['foo.jh']]).catch(error => {
mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': () => {} })([['foo.jh']]).catch(error => {
assert.noFile('foo.jh');
done();
});
});

it('should print error message', done => {
proxyquire('../../cli/jdl.cjs', { './import-jdl.cjs': () => {} })([['foo.jh']]).catch(error => {
mockRequire('../../cli/jdl.cjs', { './import-jdl.cjs': () => {} })([['foo.jh']]).catch(error => {
assert.equal(
error.message,
'Error downloading https://raw.githubusercontent.com/jhipster/jdl-samples/main/foo.jh: 404 - Custom message'
Expand Down