Skip to content

Commit 1baaa55

Browse files
committed
Added test command, dependencies, files
1 parent 534adaf commit 1baaa55

10 files changed

+2273
-312
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"env": {
1313
"browser": true,
14-
"node": true
14+
"node": true,
15+
"mocha": true
1516
},
1617
"rules": {
1718
"@typescript-eslint/no-var-requires": 0

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
2+
coverage
23
node_modules
34
.DS_Store
45
.env

.mocharc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extension": [
3+
"ts, js"
4+
],
5+
"loader": "ts-node/esm"
6+
}

package-lock.json

+2,197-306
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,41 @@
33
"version": "0.2.0",
44
"description": "Get started with Chrome extensions development using webpack, Typescript, Sass, and more",
55
"scripts": {
6-
"start": "webpack --watch --config webpack.dev.js",
7-
"build": "webpack --config webpack.prod.js",
6+
"start": "webpack --watch --config webpack.dev.cjs",
7+
"build": "webpack --config webpack.prod.cjs",
88
"lint": "eslint --ext .ts,.js --max-warnings=0 .",
9-
"prettier": "prettier --write"
9+
"prettier": "prettier --write",
10+
"test": "c8 mocha ./test/setup.js ./test/**/*.{js,ts}"
1011
},
12+
"type": "module",
1113
"license": "MIT",
1214
"devDependencies": {
1315
"@babel/core": "^7.18.6",
1416
"@babel/plugin-transform-runtime": "^7.18.6",
1517
"@babel/preset-env": "^7.18.6",
1618
"@babel/preset-typescript": "^7.18.6",
1719
"@types/chrome": "^0.0.193",
20+
"@types/mocha": "^10.0.1",
1821
"@typescript-eslint/eslint-plugin": "^5.30.5",
1922
"@typescript-eslint/parser": "^5.30.5",
2023
"babel-loader": "^8.2.5",
24+
"c8": "^7.13.0",
2125
"copy-webpack-plugin": "^11.0.0",
2226
"css-loader": "^6.7.1",
2327
"dotenv-webpack": "^8.0.0",
2428
"eslint": "^8.19.0",
2529
"eslint-config-prettier": "^8.5.0",
2630
"eslint-webpack-plugin": "^3.2.0",
31+
"expect.js": "^0.3.1",
2732
"husky": "^8.0.1",
2833
"lint-staged": "^13.0.3",
2934
"mini-css-extract-plugin": "^2.6.1",
35+
"mocha": "^10.2.0",
3036
"prettier": "^2.7.1",
3137
"sass": "^1.53.0",
3238
"sass-loader": "^13.0.2",
39+
"sinon-chrome": "^3.0.1",
40+
"ts-node": "^10.9.1",
3341
"typescript": "^4.7.4",
3442
"webpack": "^5.73.0",
3543
"webpack-cli": "^4.10.0",

test/setup.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// The chrome API won't be available in the test environment, so it has to be mocked
2+
// Of course, you can remove this setup file if you don't want to test interactions with the chrome API
3+
import sinonChrome from 'sinon-chrome';
4+
5+
global.chrome = sinonChrome;
6+
7+
let mockedStorage = {};
8+
9+
// These are just the most important methods, feel free to add more if needed
10+
chrome.storage.sync.get.callsFake(() => {
11+
return Promise.resolve(mockedStorage)
12+
});
13+
14+
chrome.storage.sync.set.callsFake((obj) => {
15+
Object.assign(mockedStorage, obj);
16+
return Promise.resolve()
17+
});
18+
19+
chrome.storage.sync.clear.callsFake(() => {
20+
for (const key in mockedStorage) {
21+
delete mockedStorage[key];
22+
}
23+
return Promise.resolve()
24+
});
25+
26+
beforeEach(() => {
27+
chrome.storage.sync.set({});
28+
});
29+
30+
afterEach(async function () {
31+
await chrome.storage.sync.clear();
32+
});

test/storage.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import expect from 'expect.js';
2+
import { getStorageData, setStorageData } from '../src/storage.js';
3+
4+
// The "npm test" command will run all test files and also create a coverage report using c8
5+
describe('storage.ts', () => {
6+
it('should be able to get storage data', async () => {
7+
getStorageData().then((data) => {
8+
expect(data).to.be.an('object');
9+
expect(data).to.be.empty();
10+
});
11+
});
12+
13+
it('should be able to set storage data', async () => {
14+
setStorageData({ test: 'test' }).then(() => {
15+
getStorageData().then((data) => {
16+
expect(data).to.be.an('object');
17+
expect(data).to.have.property('test');
18+
expect(data.test).to.be('test');
19+
});
20+
});
21+
});
22+
});
File renamed without changes.

webpack.dev.js renamed to webpack.dev.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { merge } = require('webpack-merge');
2-
const common = require('./webpack.common.js');
2+
const common = require('./webpack.common.cjs');
33

44
module.exports = merge(common, {
55
mode: 'development',

webpack.prod.js renamed to webpack.prod.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { merge } = require('webpack-merge');
2-
const common = require('./webpack.common.js');
2+
const common = require('./webpack.common.cjs');
33

44
module.exports = merge(common, {
55
mode: 'production',

0 commit comments

Comments
 (0)