Skip to content
Permalink
Browse files
Rework npm fetch test to make it faster and less involved
There was so much wrong with the previous test:
- I could not get it to fail, even w/out appending '@latest' in create
- It relies on an active network connection and can take over 20s easily
- It relies on knowing where fetch will save the downloaded template
- It seems to test some kind of cache busting that should probably
  handled by fetch instead of create anyway

The new test inherits only the last downside.
  • Loading branch information
raphinesse committed May 15, 2018
1 parent 6a864a3 commit 102ad58327a4818005720036a2971a7ade2ee92f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
@@ -43,8 +43,7 @@
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"jasmine": "^3.1.0",
"rewire": "^4.0.1",
"semver": "^5.3.0"
"rewire": "^4.0.1"
},
"scripts": {
"test": "npm run eslint && npm run jasmine",
@@ -18,11 +18,9 @@
*/

var fs = require('fs');
var os = require('os');
var path = require('path');

var shell = require('shelljs');
var semver = require('semver');
var rewire = require('rewire');
var requireFresh = require('import-fresh');

@@ -41,9 +39,6 @@ var project = path.join(tmpDir, appName);
// Disable regular console output during tests
CordovaLogger.get().setLevel(CordovaLogger.ERROR);

// Global configuration paths
var global_config_path = process.env.CORDOVA_HOME || path.join(os.homedir(), '.cordova');

function createWith (rewiring) {
return (...args) => create.__with__(rewiring)(() => create(...args));
}
@@ -210,21 +205,29 @@ describe('create end-to-end', function () {
.then(checkProject);
});

it('should successfully run with NPM package and not use old cache of template on second create', function () {
var templatePkgJsonPath = path.join(global_config_path, 'node_modules', 'phonegap-template-vue-f7-tabs', 'package.json');
it('should successfully run with NPM package', function () {
// Create a real project with npm module as template
// tests cache clearing of npm template
// uses phonegap-template-vue-f7-tabs
var configNPMold = {
var config = {
lib: {
www: {
template: true,
url: 'phonegap-template-vue-f7-tabs@1.0.0',
url: 'phonegap-template-vue-f7-tabs@1',
version: ''
}
}
};
var configNPM = {
return createWithMockFetch(project, appId, appName, config, events)
.then(fetchSpy => {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url);
})
.then(checkProject);
});

it('should successfully run with NPM package and explicitly fetch latest if no version is given', function () {
// Create a real project with npm module as template
// TODO fetch should be responsible for the cache busting part of this test
var config = {
lib: {
www: {
template: true,
@@ -233,18 +236,13 @@ describe('create end-to-end', function () {
}
}
};
return create(project, appId, appName, configNPMold)
.then(checkProject)
.then(function () {
shell.rm('-rf', project);
var pkgJson = requireFresh(templatePkgJsonPath);
expect(pkgJson.version).toBe('1.0.0');
return create(project, appId, appName, configNPM);
}).then(function () {
var pkgJson = requireFresh(templatePkgJsonPath);
expect(semver.gt(pkgJson.version, '1.0.0')).toBeTruthy();
});
}, 60000);
return createWithMockFetch(project, appId, appName, config, events)
.then(fetchSpy => {
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.calls.argsFor(0)[0]).toBe(config.lib.www.url + '@latest');
})
.then(checkProject);
});

it('should successfully run with template not having a package.json at toplevel', function () {
var config = {

0 comments on commit 102ad58

Please sign in to comment.