Skip to content

Commit

Permalink
test: jest functional tests and xo linting (#1)
Browse files Browse the repository at this point in the history
* test: added xo linting
* test: added jest functional tests
* test: added travis and appveyor config files
* fix: fixed package name when using a full path as the target dir name
  • Loading branch information
saintsebastian authored and rpl committed Jul 3, 2017
1 parent 6b4a1a9 commit 3617656
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules/
package-lock.json
16 changes: 16 additions & 0 deletions .travis.yml
@@ -0,0 +1,16 @@
language: node_js
sudo: false

# Keep this in sync with appveyor.yml
node_js:
- '6'
- '7'

before_script:
# If this command fails and you can't fix it, file an issue and add an exception to .nsprc
- npm run nsp-check

# Keep this in sync with appveyor.yml
script:
- npm run lint
- npm test
27 changes: 27 additions & 0 deletions __tests__/helpers/cmd-runner.js
@@ -0,0 +1,27 @@
const {spawn} = require("child_process");

module.exports = function cmdRunner(cmdArgs) {
const spawnedProcess = spawn(
process.execPath, cmdArgs,
);

return new Promise((resolve) => {
let errorData = "";
let outputData = "";

spawnedProcess.stderr.on("data", (data) => {
errorData += data;
});
spawnedProcess.stdout.on("data", (data) => {
outputData += data;
});

spawnedProcess.on("close", (exitCode) => {
resolve({
exitCode,
stderr: errorData,
stdout: outputData,
});
});
});
};
15 changes: 15 additions & 0 deletions __tests__/helpers/tmp-dir.js
@@ -0,0 +1,15 @@
const tmp = require("tmp");
const promisify = require("es6-promisify");

const createTempDir = promisify(tmp.dir, {multiArgs: true});

module.exports = function withTmpDir(testPromise) {
return createTempDir({
prefix: "tmp-create-web-ext-",
// This allows us to remove a non-empty tmp dir.
unsafeCleanup: true,
})
.then(([tmpPath]) => {
return testPromise(tmpPath);
});
};
71 changes: 71 additions & 0 deletions __tests__/index.test.js
@@ -0,0 +1,71 @@
"use strict";

const path = require("path");
const fs = require("mz/fs");
const linter = require("addons-linter");
const withTmpDir = require("./helpers/tmp-dir");
const cmdRunner = require("./helpers/cmd-runner");

const execDirPath = path.join(__dirname, "..", "bin");

describe("main", () => {
test("creates files including manifest with correct name", () => withTmpDir(
async (tmpPath) => {
const projName = "target";
const targetDir = path.join(tmpPath, projName);

const cmd = await cmdRunner([`${execDirPath}/create-webextension`, `${targetDir}`]);

if (cmd.exitCode !== 0) {
throw new Error(`Command Run Failed: ${cmd.stderr}`);
}

const contentStat = await fs.stat(path.join(targetDir, "content.js"));
expect(contentStat.isDirectory()).toBeFalsy();

const bgStat = await fs.stat(path.join(targetDir, "background.js"));
expect(bgStat.isDirectory()).toBeFalsy();

const manifest = await fs.readFile(path.join(targetDir, "manifest.json"), "utf-8");
const parsed = JSON.parse(manifest);
expect(parsed.name).toEqual(projName);
})
);

test("created project is linted correctly", () => withTmpDir(
async (tmpPath) => {
const projName = "target";
const targetDir = path.join(tmpPath, projName);

const cmd = await cmdRunner([`${execDirPath}/create-webextension`, `${targetDir}`]);

if (cmd.exitCode !== 0) {
throw new Error(`Command Run Failed: ${cmd.stderr}`);
}

const config = {
_: [targetDir],
logLevel: "fatal",
stack: true,
pretty: true,
warningsAsErrors: false,
metadata: false,
output: "none",
boring: false,
selfHosted: false,
};
const linterInstance = linter.createInstance({
config,
runAsBinary: false,
});
const summary = {
errors: 0,
notices: 0,
warnings: 0,
};

const instance = await linterInstance.run();
expect(instance.summary).toEqual(summary);
})
);
});
23 changes: 23 additions & 0 deletions appveyor.yml
@@ -0,0 +1,23 @@
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
- set PATH=%APPDATA%\npm;%PATH%
- npm install

matrix:
fast_finish: true

build: off
shallow_clone: true

# Keep the following configs in sync with .travis.yml
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'

test_script:
- node --version
- npm --version
- npm run lint
- npm run test
23 changes: 10 additions & 13 deletions index.js
@@ -1,8 +1,8 @@
"use strict";

const path = require("path");
const chalk = require("chalk");
const fs = require("mz/fs");
const path = require("path");
const stripAnsi = require("strip-ansi");

const USAGE_MSG = `Usage: create-webextension project_dir_name`;
Expand Down Expand Up @@ -50,12 +50,11 @@ Congratulations!!! A new WebExtension has been created at:

function getProjectReadme(projectDirName) {
return fs.readFile(path.join(__dirname, "assets", "webextension-logo.ascii"))
.then(asciiLogo => {
.then(() => {
return `# ${projectDirName}\n${README}${MORE_INFO_MSG}`;
});
}


function getPlaceholderIcon() {
return fs.readFile(path.join(__dirname, "assets", "icon.png"));
}
Expand All @@ -69,36 +68,34 @@ function getProjectManifest(projectDirName) {
content_scripts: [
{
matches: ["https://developer.mozilla.org/*"],
js: ['content.js'],
js: ["content.js"],
},
],
permissions: [],
icons: {
'64': 'icon.png',
"64": "icon.png",
},
browser_action: {
default_title: `${projectDirName} (browserAction)`,
default_icon: {
'64': 'icon.png',
"64": "icon.png",
},
},
background: {
scripts: ['background.js'],
scripts: ["background.js"],
},
};
}

exports.main = function main() {
const projectDirName = process.argv[2];
let projectPath;

if (!projectDirName) {
if (!process.argv[2]) {
console.error(`${chalk.red("Missing project dir name.")}\n`);
console.log(USAGE_MSG);
process.exit(1);
}

projectPath = path.resolve(projectDirName);
const projectPath = path.resolve(process.argv[2]);
const projectDirName = path.basename(projectPath);

return fs.mkdir(projectPath).then(() => {
return Promise.all([
Expand All @@ -125,4 +122,4 @@ exports.main = function main() {
console.error(error);
process.exit(1);
});
}
};
85 changes: 84 additions & 1 deletion package.json
Expand Up @@ -7,7 +7,9 @@
},
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest",
"lint": "xo",
"nsp-check": "nsp check -o summary"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +23,9 @@
"addon"
],
"author": "Luca Greco <lgreco@mozilla.com>",
"contributors": [
"Elvina Valieva <elvinavalieva@gmail.com>"
],
"license": "MPL-2.0",
"bugs": {
"url": "https://github.com/rpl/create-webextension/issues"
Expand All @@ -30,5 +35,83 @@
"chalk": "^1.1.3",
"mz": "^2.6.0",
"strip-ansi": "^3.0.1"
},
"devDependencies": {
"addons-linter": "^0.22.3",
"babel-jest": "^20.0.3",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"es6-promisify": "^5.0.0",
"eslint-plugin-jest": "^20.0.3",
"jest": "^20.0.4",
"nsp": "^2.6.3",
"regenerator-runtime": "^0.10.5",
"tmp": "0.0.31",
"xo": "^0.18.2"
},
"jest": {
"transform": {
".*": "<rootDir>/node_modules/babel-jest"
},
"testEnvironment": "node",
"testMatch": [
"<rootDir>/__tests__/**/*.test.js",
"<rootDir>/__tests__/*.test.js"
],
"testPathIgnorePatterns": [
"node_modules/"
]
},
"babel": {
"compact": false,
"presets": [
"es2015",
"stage-2"
],
"plugins": [
[
"transform-runtime",
{
"polyfill": false,
"regenerator": true
}
]
]
},
"xo": {
"extends": [
"plugin:jest/recommended"
],
"plugins": [
"jest"
],
"envs": [
"jest/globals"
],
"space": true,
"ignores": [
"assets/**/*",
"node_modules/**/*"
],
"rules": {
"quotes": [
"error",
"double",
{
"allowTemplateLiterals": true
}
],
"comma-dangle": [
"error",
"always-multiline"
],
"camelcase": 0,
"arrow-parens": 0,
"unicorn/no-process-exit": 0,
"quote-props": 0,
"unicorn/catch-error-name": 0,
"func-names": 0
}
}
}

0 comments on commit 3617656

Please sign in to comment.