Skip to content

Commit 7f3ba78

Browse files
jsjoeiojevakallio
andauthoredAug 4, 2020
feat(foam-vscode): add jest for testing (#204)
* feat: setup jest for foam-vscode * Add failing test (fails due to missing vscode dependency) * feat: add vscode mock Co-authored-by: Jani Eväkallio <jani.evakallio@gmail.com>
1 parent bcab419 commit 7f3ba78

File tree

7 files changed

+1019
-6
lines changed

7 files changed

+1019
-6
lines changed
 

‎.github/workflows/foam-vscode.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020

2121
- name: Lint foam-vscode
2222
run: yarn workspace foam-vscode lint
23-
# - name: Test foam-vscode
24-
# run: yarn workspace foam-vscode test
23+
- name: Test foam-vscode
24+
run: yarn workspace foam-vscode test
2525
# - name: Publish foam-vscode
2626
# if: github.ref == 'refs/heads/master'
2727
# run: yarn workspace foam-vscode publish-extension
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Note: this is needed in order to test certain parts
3+
of functionality of `foam-vscode`
4+
5+
Following the advice from this article:
6+
https://www.richardkotze.com/coding/unit-test-mock-vs-code-extension-api-jest
7+
8+
combined with advice from this GitHub issue comment:
9+
https://github.com/microsoft/vscode-test/issues/37#issuecomment-584744386
10+
*/
11+
12+
const vscode = {
13+
// Add values and methods as needed for tests
14+
};
15+
16+
module.exports = vscode;

‎packages/foam-vscode/babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript"
5+
]
6+
};

‎packages/foam-vscode/jest.config.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// The directory where Jest should store its cached dependency information
12+
// cacheDirectory: "/private/var/folders/p6/5y5l8tbs1d32pq9b596lk48h0000gn/T/jest_dx",
13+
14+
// Automatically clear mock calls and instances between every test
15+
clearMocks: true,
16+
17+
// Indicates whether the coverage information should be collected while executing the test
18+
// collectCoverage: false,
19+
20+
// An array of glob patterns indicating a set of files for which coverage information should be collected
21+
// collectCoverageFrom: undefined,
22+
23+
// The directory where Jest should output its coverage files
24+
// coverageDirectory: undefined,
25+
26+
// An array of regexp pattern strings used to skip coverage collection
27+
// coveragePathIgnorePatterns: [
28+
// "/node_modules/"
29+
// ],
30+
31+
// Indicates which provider should be used to instrument code for coverage
32+
// coverageProvider: "babel",
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: undefined,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: undefined,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: undefined,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
// moduleFileExtensions: [
73+
// "js",
74+
// "json",
75+
// "jsx",
76+
// "ts",
77+
// "tsx",
78+
// "node"
79+
// ],
80+
81+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
82+
// moduleNameMapper: {},
83+
84+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
85+
// modulePathIgnorePatterns: [],
86+
87+
// Activates notifications for test results
88+
// notify: false,
89+
90+
// An enum that specifies notification mode. Requires { notify: true }
91+
// notifyMode: "failure-change",
92+
93+
// A preset that is used as a base for Jest's configuration
94+
// preset: undefined,
95+
96+
// Run tests from one or more projects
97+
// projects: undefined,
98+
99+
// Use this configuration option to add custom reporters to Jest
100+
// reporters: undefined,
101+
102+
// Automatically reset mock state between every test
103+
// resetMocks: false,
104+
105+
// Reset the module registry before running each individual test
106+
// resetModules: false,
107+
108+
// A path to a custom resolver
109+
// resolver: undefined,
110+
111+
// Automatically restore mock state between every test
112+
// restoreMocks: false,
113+
114+
// The root directory that Jest should scan for tests and modules within
115+
// rootDir: undefined,
116+
117+
// A list of paths to directories that Jest should use to search for files in
118+
// roots: [
119+
// "<rootDir>"
120+
// ],
121+
122+
// Allows you to use a custom runner instead of Jest's default test runner
123+
// runner: "jest-runner",
124+
125+
// The paths to modules that run some code to configure or set up the testing environment before each test
126+
// setupFiles: [],
127+
128+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129+
// setupFilesAfterEnv: [],
130+
131+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132+
// snapshotSerializers: [],
133+
134+
// The test environment that will be used for testing
135+
testEnvironment: "node"
136+
137+
// Options that will be passed to the testEnvironment
138+
// testEnvironmentOptions: {},
139+
140+
// Adds a location field to test results
141+
// testLocationInResults: false,
142+
143+
// The glob patterns Jest uses to detect test files
144+
// testMatch: [
145+
// "**/__tests__/**/*.[jt]s?(x)",
146+
// "**/?(*.)+(spec|test).[tj]s?(x)"
147+
// ],
148+
149+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
150+
// testPathIgnorePatterns: [
151+
// "/node_modules/"
152+
// ],
153+
154+
// The regexp pattern or array of patterns that Jest uses to detect test files
155+
// testRegex: [],
156+
157+
// This option allows the use of a custom results processor
158+
// testResultsProcessor: undefined,
159+
160+
// This option allows use of a custom test runner
161+
// testRunner: "jasmine2",
162+
163+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
164+
// testURL: "http://localhost",
165+
166+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
167+
// timers: "real",
168+
169+
// A map from regular expressions to paths to transformers
170+
// transform: undefined,
171+
172+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
173+
// transformIgnorePatterns: [
174+
// "/node_modules/"
175+
// ],
176+
177+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
178+
// unmockedModulePathPatterns: undefined,
179+
180+
// Indicates whether each individual test should be reported during the run
181+
// verbose: undefined,
182+
183+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
184+
// watchPathIgnorePatterns: [],
185+
186+
// Whether to use watchman for file crawling
187+
// watchman: true,
188+
};

‎packages/foam-vscode/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
},
9090
"scripts": {
9191
"build": "tsc -p ./",
92-
"test": "echo No tests in VSCode extensions yet",
92+
"test": "jest",
9393
"lint": "eslint src --ext ts",
9494
"clean": "tsc --build ./tsconfig.json ../foam-core/tsconfig.json --clean",
9595
"watch": "tsc --build ./tsconfig.json ../foam-core/tsconfig.json --watch",
@@ -101,14 +101,19 @@
101101
"publish-extension": "npx vsce publish && yarn npm-cleanup"
102102
},
103103
"devDependencies": {
104+
"@babel/core": "^7.11.0",
105+
"@babel/preset-env": "^7.11.0",
106+
"@babel/preset-typescript": "^7.10.4",
104107
"@types/dateformat": "^3.0.1",
105108
"@types/glob": "^7.1.1",
106109
"@types/node": "^13.11.0",
107110
"@types/vscode": "^1.45.1",
108111
"@typescript-eslint/eslint-plugin": "^2.30.0",
109112
"@typescript-eslint/parser": "^2.30.0",
113+
"babel-jest": "^26.2.2",
110114
"eslint": "^6.8.0",
111115
"glob": "^7.1.6",
116+
"jest": "^26.2.2",
112117
"rimraf": "^3.0.2",
113118
"typescript": "^3.8.3",
114119
"vscode-test": "^1.3.0"
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @note: This will fail due to utils importing 'vscode'
2+
// which needs to be mocked in the jest test environment.
3+
// See: https://github.com/microsoft/vscode-test/issues/37
4+
import { dropExtension } from '../src/utils';
5+
6+
describe("dropExtension", () => {
7+
test("returns file name without extension", () => {
8+
expect(dropExtension('file.md')).toEqual('file');
9+
});
10+
});

0 commit comments

Comments
 (0)