@universal-packages/workflows-jest 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present David De Anda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Workflows Jest
2
+
3
+ [![npm version](https://badge.fury.io/js/@universal-packages%2Fworkflows-jest.svg)](https://www.npmjs.com/package/@universal-packages/workflows-jest)
4
+ [![Testing](https://github.com/universal-packages/universal-workflows-jest/actions/workflows/testing.yml/badge.svg)](https://github.com/universal-packages/universal-workflows-jest/actions/workflows/testing.yml)
5
+ [![codecov](https://codecov.io/gh/universal-packages/universal-workflows-jest/branch/main/graph/badge.svg?token=CXPJSN8IGL)](https://codecov.io/gh/universal-packages/universal-workflows-jest)
6
+
7
+ Jest matchers for [Workflows](https://github.com/universal-packages/universal-workflows) testing.
8
+
9
+ ## Install
10
+
11
+ ```shell
12
+ npm install @universal-packages/workflows-jest
13
+
14
+ npm install @universal-packages/workflows
15
+ ```
16
+
17
+ ## Setup
18
+
19
+ Add the following to your `jest.config.js` or where you configure Jest:
20
+
21
+ ```js
22
+ module.exports = {
23
+ setupFilesAfterEnv: ['@universal-packages/workflows-jest']
24
+ }
25
+ ```
26
+
27
+ ## Matchers
28
+
29
+ ### toHaveFinishWithStatus
30
+
31
+ ```js
32
+ import { Workflows } from '@universal-packages/workflows'
33
+
34
+ it('should be successful', async () => {
35
+ const workflow = await workflowsJest.run('my-workflow')
36
+
37
+ expect(workflow).toHaveFinishWithStatus('success')
38
+ })
39
+ ```
40
+
41
+ ### toHaveBeenBuildAndRun
42
+
43
+ ```js
44
+ import { runApp } from './src'
45
+
46
+ workflowsJest.mockRuns()
47
+
48
+ it('should have been build and run', async () => {
49
+ await runApp({ development: true })
50
+
51
+ expect('development-workflow').toHaveBeenBuildAndRun()
52
+ })
53
+ ```
54
+
55
+ ### toHaveBeenBuildAndRunWithVariables
56
+
57
+ ```js
58
+ import { runApp } from './src'
59
+
60
+ workflowsJest.mockRuns()
61
+
62
+ it('should have been build and run', async () => {
63
+ await runApp({ development: true, fast: true })
64
+
65
+ expect('development-workflow').toHaveBeenBuildAndRunWithVariables({ fast: true })
66
+ })
67
+ ```
68
+
69
+ ## Typescript
70
+
71
+ In order for typescript to see the global types you need to reference the types somewhere in your project, normally `./src/globals.d.ts`.
72
+
73
+ ```ts
74
+ /// <reference types="@universal-packages/workflows-jest" />
75
+ ```
76
+
77
+ This library is developed in TypeScript and shipped fully typed.
78
+
79
+ ## Contributing
80
+
81
+ The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
82
+
83
+ - [Code of Conduct](./CODE_OF_CONDUCT.md)
84
+ - [Contributing Guide](./CONTRIBUTING.md)
85
+
86
+ ### License
87
+
88
+ [MIT licensed](./LICENSE).
package/globals.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { BuildFromOptions, Status, Workflow } from '@universal-packages/workflows';
2
+ interface ProcessMockDescriptor {
3
+ command: string;
4
+ env?: Record<string, string>;
5
+ workingDirectory?: string;
6
+ result: string;
7
+ }
8
+ interface WorkflowJestRunOptions extends BuildFromOptions {
9
+ targetMockResults?: ProcessMockDescriptor[];
10
+ }
11
+ declare global {
12
+ namespace workflowsJest {
13
+ function run(name: string, options?: WorkflowJestRunOptions): Promise<Workflow>;
14
+ function mockRuns(): void;
15
+ }
16
+ namespace jest {
17
+ interface Matchers<R> {
18
+ toHaveFinishWithStatus(status: Status): R;
19
+ toHaveBeenBuildAndRun(): R;
20
+ toHaveBeenBuildAndRunWithVariables(variables: Record<string, any>): R;
21
+ }
22
+ }
23
+ }
24
+ export {};
package/globals.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=globals.js.map
package/globals.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globals.js","sourceRoot":"","sources":["../src/globals.ts"],"names":[],"mappings":""}
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ import './globals';
package/index.js ADDED
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const globals_1 = require("@jest/globals");
4
+ const sub_process_1 = require("@universal-packages/sub-process");
5
+ const workflows_1 = require("@universal-packages/workflows");
6
+ require("./globals");
7
+ const WORKFLOWS_JEST = {
8
+ mocked: false,
9
+ ranMockedWorkflows: []
10
+ };
11
+ global.workflowsJest = {
12
+ run: async (name, options) => {
13
+ if (options?.targetMockResults) {
14
+ const { targetMockResults } = options;
15
+ for (let i = 0; i < targetMockResults.length; i++) {
16
+ const currentTargetMockResult = targetMockResults[i];
17
+ const commandParts = currentTargetMockResult.command.split(' ');
18
+ const actualCommand = commandParts[0];
19
+ const actualArgs = commandParts.slice(1);
20
+ sub_process_1.TestEngine.mockProcessEvents({
21
+ command: actualCommand,
22
+ args: actualArgs,
23
+ env: currentTargetMockResult.env,
24
+ workingDirectory: currentTargetMockResult.workingDirectory,
25
+ events: [{ type: 'stdout', data: currentTargetMockResult.result }]
26
+ });
27
+ }
28
+ }
29
+ const workflow = workflows_1.Workflow.buildFrom(name, options);
30
+ workflow.on('**', jest.fn());
31
+ await workflow.run();
32
+ return workflow;
33
+ },
34
+ mockRuns: () => {
35
+ WORKFLOWS_JEST.mocked = true;
36
+ workflows_1.Workflow.buildFrom = (name, options) => {
37
+ return {
38
+ run: () => {
39
+ WORKFLOWS_JEST.ranMockedWorkflows.push({ name, options });
40
+ }
41
+ };
42
+ };
43
+ }
44
+ };
45
+ beforeEach(() => {
46
+ sub_process_1.TestEngine.reset();
47
+ WORKFLOWS_JEST.ranMockedWorkflows = [];
48
+ });
49
+ function toHaveFinishWithStatus(workflow, status) {
50
+ const pass = workflow.status === status;
51
+ if (pass) {
52
+ return {
53
+ message: () => `expected "${workflow.name || 'Workflow'}" not to have finished with status ${this.utils.printExpected(status)}`,
54
+ pass
55
+ };
56
+ }
57
+ else {
58
+ return {
59
+ message: () => {
60
+ const received = workflow.status;
61
+ return `expected "${workflow.name || 'Workflow'}" to have finished with status ${this.utils.printExpected(status)}, but it finished with status ${this.utils.printReceived(received)}`;
62
+ },
63
+ pass
64
+ };
65
+ }
66
+ }
67
+ function toHaveBeenBuildAndRun(workflow) {
68
+ const pass = WORKFLOWS_JEST.ranMockedWorkflows.some((ranWorkflow) => ranWorkflow.name === workflow);
69
+ if (pass) {
70
+ return {
71
+ message: () => `expected "${workflow}" not to have been build and run, but it was`,
72
+ pass
73
+ };
74
+ }
75
+ else {
76
+ return {
77
+ message: () => `expected "${workflow}" to have been build and run, but it was not`,
78
+ pass
79
+ };
80
+ }
81
+ }
82
+ function toHaveBeenBuildAndRunWithVariables(workflow, variables) {
83
+ const runInstances = WORKFLOWS_JEST.ranMockedWorkflows.filter((ranWorkflow) => ranWorkflow.name === workflow);
84
+ const pass = runInstances.some((ranWorkflow) => this.equals(ranWorkflow.options?.variables, variables));
85
+ if (pass) {
86
+ return {
87
+ message: () => `expected "${workflow}" not to have been build and run with variables ${this.utils.printExpected(variables)}, but it was`,
88
+ pass
89
+ };
90
+ }
91
+ else {
92
+ if (runInstances.length === 0) {
93
+ return {
94
+ message: () => `expected "${workflow}" to have been build and run with variables ${this.utils.printExpected(variables)}, but it was not build and run at all`,
95
+ pass
96
+ };
97
+ }
98
+ else {
99
+ return {
100
+ message: () => `expected "${workflow}" to have been build and run with variables ${this.utils.printExpected(variables)}, but it was not`,
101
+ pass
102
+ };
103
+ }
104
+ }
105
+ }
106
+ globals_1.expect.extend({
107
+ toHaveFinishWithStatus,
108
+ toHaveBeenBuildAndRun,
109
+ toHaveBeenBuildAndRunWithVariables
110
+ });
111
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,2CAAsC;AACtC,iEAA4D;AAC5D,6DAAkF;AAElF,qBAAkB;AAElB,MAAM,cAAc,GAAG;IACrB,MAAM,EAAE,KAAK;IACb,kBAAkB,EAAE,EAAE;CACvB,CAAA;AAED,MAAM,CAAC,aAAa,GAAG;IACrB,GAAG,EAAE,KAAK,EAAE,IAAY,EAAE,OAAQ,EAAqB,EAAE;QACvD,IAAI,OAAO,EAAE,iBAAiB,EAAE;YAC9B,MAAM,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAA;YAErC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,iBAAiB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBACjD,MAAM,uBAAuB,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAA;gBACpD,MAAM,YAAY,GAAG,uBAAuB,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBAC/D,MAAM,aAAa,GAAG,YAAY,CAAC,CAAC,CAAC,CAAA;gBACrC,MAAM,UAAU,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAA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AAa,QAAQ,mDAAmD,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,SAAS,CAAC,cAAc;YACxI,IAAI;SACL,CAAA;KACF;SAAM;QACL,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE;YAC7B,OAAO;gBACL,OAAO,EAAE,GAAG,EAAE,CAAC,aAAa,QAAQ,+CAA+C,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,SAAS,CAAC,uCAAuC;gBAC7J,IAAI;aACL,CAAA;SACF;aAAM;YACL,OAAO;gBACL,OAAO,EAAE,GAAG,EAAE,CAAC,aAAa,QAAQ,+CAA+C,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,SAAS,CAAC,kBAAkB;gBACxI,IAAI;aACL,CAAA;SACF;KACF;AACH,CAAC;AAED,gBAAM,CAAC,MAAM,CAAC;IACZ,sBAAsB;IACtB,qBAAqB;IACrB,kCAAkC;CACnC,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@universal-packages/workflows-jest",
3
+ "version": "1.0.0",
4
+ "description": "Jest matchers for universal workflows",
5
+ "author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "types": "index.d.ts",
9
+ "repository": "git@github.com:universal-packages/universal-workflows-jest.git",
10
+ "scripts": {
11
+ "build": "tsc --p tsconfig.dis.json",
12
+ "test": "jest --watch",
13
+ "test:full": "jest --coverage --verbose",
14
+ "test:clear": "jest --clearCache",
15
+ "format": "prettier --write \"./{src,tests}/**/*.{ts,tsx,js,jsx,json}\"",
16
+ "update-dependents": "umaintenance update-dependents"
17
+ },
18
+ "peerDependencies": {
19
+ "@universal-packages/workflows": "^1.6.0"
20
+ },
21
+ "devDependencies": {
22
+ "@trivago/prettier-plugin-sort-imports": "^4.2.0",
23
+ "@types/jest": "^29.5.5",
24
+ "@types/node": "^18.11.9",
25
+ "@universal-packages/maintenance": "^1.2.5",
26
+ "jest": "^29.7.0",
27
+ "prettier": "^3.0.3",
28
+ "strip-ansi": "^6.0.0",
29
+ "ts-jest": "^29.1.1",
30
+ "typescript": "^5.2.2"
31
+ },
32
+ "jest": {
33
+ "preset": "ts-jest",
34
+ "collectCoverageFrom": [
35
+ "src/**/*.ts"
36
+ ],
37
+ "setupFilesAfterEnv": [
38
+ "<rootDir>/tests/setup.ts"
39
+ ]
40
+ },
41
+ "prettier": {
42
+ "semi": false,
43
+ "singleQuote": true,
44
+ "printWidth": 180,
45
+ "trailingComma": "none",
46
+ "plugins": [
47
+ "@trivago/prettier-plugin-sort-imports"
48
+ ],
49
+ "importOrder": [
50
+ "^[./]"
51
+ ],
52
+ "importOrderSeparation": true,
53
+ "importOrderSortSpecifiers": true,
54
+ "importOrderParserPlugins": [
55
+ "typescript",
56
+ "jsx",
57
+ "classProperties",
58
+ "decorators-legacy"
59
+ ]
60
+ }
61
+ }