@friggframework/devtools 1.2.0-canary.293.50b9cd8.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +3 -0
- package/CHANGELOG.md +117 -0
- package/README.md +80 -0
- package/frigg-cli/backendJs.js +33 -0
- package/frigg-cli/backendPath.js +26 -0
- package/frigg-cli/commitChanges.js +16 -0
- package/frigg-cli/environmentVariables.js +134 -0
- package/frigg-cli/environmentVariables.test.js +86 -0
- package/frigg-cli/index.js +14 -0
- package/frigg-cli/index.test.js +109 -0
- package/frigg-cli/installCommand.js +57 -0
- package/frigg-cli/installPackage.js +13 -0
- package/frigg-cli/integrationFile.js +30 -0
- package/frigg-cli/logger.js +12 -0
- package/frigg-cli/template.js +90 -0
- package/frigg-cli/validatePackage.js +79 -0
- package/index.js +2 -6
- package/package.json +16 -7
- package/{test-environment → test}/auther-definition-tester.js +9 -5
- package/test/index.js +11 -0
- package/test/mock-api-readme.md +102 -0
- package/test/mock-api.js +284 -0
- package/{test-environment → test}/mock-integration.js +10 -8
- package/migrations/README.md +0 -3
- package/migrations/bump3.txt +0 -0
- package/migrations/jest.config.js +0 -3
- package/test-environment/Authenticator.js +0 -74
- package/test-environment/index.js +0 -25
- /package/{test-environment → test}/auther-definition-method-tester.js +0 -0
- /package/{test-environment → test}/integration-validator.js +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { installPackage } = require('./installPackage');
|
|
2
|
+
const { createIntegrationFile } = require('./integrationFile');
|
|
3
|
+
const { resolve } = require('node:path');
|
|
4
|
+
const { updateBackendJsFile } = require('./backendJs');
|
|
5
|
+
const { logInfo, logError } = require('./logger');
|
|
6
|
+
const { commitChanges } = require('./commitChanges');
|
|
7
|
+
const {
|
|
8
|
+
findNearestBackendPackageJson,
|
|
9
|
+
validateBackendPath,
|
|
10
|
+
} = require('./backendPath');
|
|
11
|
+
const { handleEnvVariables } = require('./environmentVariables');
|
|
12
|
+
const {
|
|
13
|
+
validatePackageExists,
|
|
14
|
+
searchAndSelectPackage,
|
|
15
|
+
} = require('./validatePackage');
|
|
16
|
+
|
|
17
|
+
const installCommand = async (apiModuleName) => {
|
|
18
|
+
try {
|
|
19
|
+
const packageNames = await searchAndSelectPackage(apiModuleName);
|
|
20
|
+
if (!packageNames || packageNames.length === 0) return;
|
|
21
|
+
|
|
22
|
+
const backendPath = findNearestBackendPackageJson();
|
|
23
|
+
validateBackendPath(backendPath);
|
|
24
|
+
|
|
25
|
+
for (const packageName of packageNames) {
|
|
26
|
+
await validatePackageExists(packageName);
|
|
27
|
+
installPackage(backendPath, packageName);
|
|
28
|
+
|
|
29
|
+
const modulePath = resolve(
|
|
30
|
+
backendPath,
|
|
31
|
+
`../../node_modules/${packageName}`
|
|
32
|
+
);
|
|
33
|
+
const {
|
|
34
|
+
Config: { label },
|
|
35
|
+
Api: ApiClass,
|
|
36
|
+
} = require(modulePath);
|
|
37
|
+
|
|
38
|
+
const sanitizedLabel = label.replace(
|
|
39
|
+
/[<>:"/\\|?*\x00-\x1F\s]/g,
|
|
40
|
+
''
|
|
41
|
+
); // Remove invalid characters and spaces console.log('Installing integration for:', sanitizedLabel);
|
|
42
|
+
createIntegrationFile(backendPath, sanitizedLabel, ApiClass);
|
|
43
|
+
updateBackendJsFile(backendPath, sanitizedLabel);
|
|
44
|
+
commitChanges(backendPath, sanitizedLabel);
|
|
45
|
+
logInfo(
|
|
46
|
+
`Successfully installed ${packageName} and updated the project.`
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
await handleEnvVariables(backendPath, modulePath);
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
logError('An error occurred:', error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
module.exports = { installCommand };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
function installPackage(backendPath, packageName) {
|
|
5
|
+
execSync(`npm install ${packageName}`, {
|
|
6
|
+
cwd: path.dirname(backendPath),
|
|
7
|
+
stdio: 'inherit',
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
installPackage,
|
|
13
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { logInfo } = require('./logger');
|
|
4
|
+
const { getIntegrationTemplate } = require('./template');
|
|
5
|
+
const INTEGRATIONS_DIR = 'src/integrations';
|
|
6
|
+
|
|
7
|
+
function createIntegrationFile(backendPath, apiModuleName, ApiClass) {
|
|
8
|
+
const integrationDir = path.join(
|
|
9
|
+
path.dirname(backendPath),
|
|
10
|
+
INTEGRATIONS_DIR
|
|
11
|
+
);
|
|
12
|
+
logInfo(`Ensuring directory exists: ${integrationDir}`);
|
|
13
|
+
fs.ensureDirSync(integrationDir);
|
|
14
|
+
|
|
15
|
+
const integrationFilePath = path.join(
|
|
16
|
+
integrationDir,
|
|
17
|
+
`${apiModuleName}Integration.js`
|
|
18
|
+
);
|
|
19
|
+
logInfo(`Writing integration file: ${integrationFilePath}`);
|
|
20
|
+
const integrationTemplate = getIntegrationTemplate(
|
|
21
|
+
apiModuleName,
|
|
22
|
+
backendPath,
|
|
23
|
+
ApiClass
|
|
24
|
+
);
|
|
25
|
+
fs.writeFileSync(integrationFilePath, integrationTemplate);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
createIntegrationFile,
|
|
30
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
function getIntegrationTemplate(apiModuleName, backendPath, ApiClass) {
|
|
4
|
+
// Find the sample data method
|
|
5
|
+
const apiMethods = Object.getOwnPropertyNames(ApiClass.prototype);
|
|
6
|
+
const sampleDataMethod =
|
|
7
|
+
apiMethods.find(
|
|
8
|
+
(method) =>
|
|
9
|
+
method.toLowerCase().includes('search') ||
|
|
10
|
+
method.toLowerCase().includes('list') ||
|
|
11
|
+
method.toLowerCase().includes('get')
|
|
12
|
+
) || 'searchDeals';
|
|
13
|
+
|
|
14
|
+
return `const { get, IntegrationBase, Options } = require('@friggframework/core');
|
|
15
|
+
const { Definition: ${apiModuleName}Module, Config: defaultConfig } = require('@friggframework/api-module-${apiModuleName.toLowerCase()}');
|
|
16
|
+
|
|
17
|
+
class ${apiModuleName}Integration extends IntegrationBase {
|
|
18
|
+
static Config = {
|
|
19
|
+
name: defaultConfig.name || '${apiModuleName.toLowerCase()}',
|
|
20
|
+
version: '1.0.0',
|
|
21
|
+
supportedVersions: ['1.0.0'],
|
|
22
|
+
events: ['SEARCH_DEALS'],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
static Options =
|
|
26
|
+
new Options({
|
|
27
|
+
module: ${apiModuleName}Module,
|
|
28
|
+
integrations: [${apiModuleName}Module],
|
|
29
|
+
display: {
|
|
30
|
+
name: defaultConfig.displayName || '${apiModuleName}',
|
|
31
|
+
description: defaultConfig.description || 'Sales & CRM, Marketing',
|
|
32
|
+
category: defaultConfig.category || 'Sales & CRM, Marketing',
|
|
33
|
+
detailsUrl: defaultConfig.detailsUrl || 'https://www.${apiModuleName.toLowerCase()}.com',
|
|
34
|
+
icon: defaultConfig.icon || 'https://friggframework.org/assets/img/${apiModuleName.toLowerCase()}.jpeg',
|
|
35
|
+
},
|
|
36
|
+
hasUserConfig: true,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
static modules = {
|
|
40
|
+
${apiModuleName.toLowerCase()}: ${apiModuleName}Module,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* HANDLE EVENTS
|
|
45
|
+
*/
|
|
46
|
+
async receiveNotification(notifier, event, object = null) {
|
|
47
|
+
if (event === 'SEARCH_DEALS') {
|
|
48
|
+
return this.target.api.searchDeals(object);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* ALL CUSTOM/OPTIONAL METHODS FOR AN INTEGRATION MANAGER
|
|
54
|
+
*/
|
|
55
|
+
async getSampleData() {
|
|
56
|
+
const res = await this.target.api.${sampleDataMethod}();
|
|
57
|
+
return { data: res };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* ALL REQUIRED METHODS FOR AN INTEGRATION MANAGER
|
|
62
|
+
*/
|
|
63
|
+
async onCreate(params) {
|
|
64
|
+
// Validate that we have all of the data we need
|
|
65
|
+
// Set integration status as makes sense. Default ENABLED
|
|
66
|
+
// TODO turn this into a validateConfig method/function
|
|
67
|
+
this.record.status = 'ENABLED';
|
|
68
|
+
await this.record.save();
|
|
69
|
+
return this.record;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async onUpdate(params) {
|
|
73
|
+
const newConfig = get(params, 'config');
|
|
74
|
+
const oldConfig = this.record.config;
|
|
75
|
+
// Just save whatever
|
|
76
|
+
this.record.markModified('config');
|
|
77
|
+
await this.record.save();
|
|
78
|
+
return this.validateConfig();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async getConfigOptions() {
|
|
82
|
+
const options = {}
|
|
83
|
+
return options;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = ${apiModuleName}Integration;`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = { getIntegrationTemplate };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
|
+
const axios = require('axios');
|
|
3
|
+
const { logError } = require('./logger');
|
|
4
|
+
const inquirer = require('inquirer');
|
|
5
|
+
|
|
6
|
+
async function searchPackages(apiModuleName) {
|
|
7
|
+
const searchCommand = `npm search @friggframework/api-module-${apiModuleName} --json`;
|
|
8
|
+
const result = execSync(searchCommand, { encoding: 'utf8' });
|
|
9
|
+
return JSON.parse(result);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function checkPackageExists(packageName) {
|
|
13
|
+
try {
|
|
14
|
+
const response = await axios.get(
|
|
15
|
+
`https://registry.npmjs.org/${packageName}`
|
|
16
|
+
);
|
|
17
|
+
return response.status === 200;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function validatePackageExists(packageName) {
|
|
24
|
+
const packageExists = await checkPackageExists(packageName);
|
|
25
|
+
if (!packageExists) {
|
|
26
|
+
throw new Error(`Package ${packageName} does not exist on npm.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const searchAndSelectPackage = async (apiModuleName) => {
|
|
31
|
+
const searchResults = await searchPackages(apiModuleName || '');
|
|
32
|
+
|
|
33
|
+
if (searchResults.length === 0) {
|
|
34
|
+
logError(`No packages found matching ${apiModuleName}`);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const filteredResults = searchResults.filter((pkg) => {
|
|
39
|
+
const version = pkg.version
|
|
40
|
+
? pkg.version.split('.').map(Number)
|
|
41
|
+
: [];
|
|
42
|
+
return version[0] >= 1;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (filteredResults.length === 0) {
|
|
46
|
+
const earlierVersions = searchResults
|
|
47
|
+
.map((pkg) => `${pkg.name} (${pkg.version})`)
|
|
48
|
+
.join(', ');
|
|
49
|
+
logError(
|
|
50
|
+
`No packages found with version 1.0.0 or above for ${apiModuleName}. Found earlier versions: ${earlierVersions}`
|
|
51
|
+
);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const choices = filteredResults.map((pkg) => {
|
|
56
|
+
return {
|
|
57
|
+
name: `${pkg.name} (${pkg.version})`,
|
|
58
|
+
checked: filteredResults.length === 1, // Automatically select if only one result
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const { selectedPackages } = await inquirer.prompt([
|
|
63
|
+
{
|
|
64
|
+
type: 'checkbox',
|
|
65
|
+
name: 'selectedPackages',
|
|
66
|
+
message: 'Select the packages to install:',
|
|
67
|
+
choices,
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
return selectedPackages.map(choice => choice.split(' ')[0]);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
validatePackageExists,
|
|
76
|
+
checkPackageExists,
|
|
77
|
+
searchPackages,
|
|
78
|
+
searchAndSelectPackage,
|
|
79
|
+
};
|
package/index.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const prettierConfig = require('../../../utils/prettier-config')
|
|
3
|
-
const testEnvironment = require('./test-environment/index');
|
|
1
|
+
const test = require('./test');
|
|
4
2
|
|
|
5
3
|
module.exports = {
|
|
6
|
-
|
|
7
|
-
prettierConfig,
|
|
8
|
-
...testEnvironment
|
|
4
|
+
...test
|
|
9
5
|
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/devtools",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "1.2.0
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@babel/eslint-parser": "^7.18.9",
|
|
7
|
+
"@babel/parser": "^7.25.3",
|
|
8
|
+
"@babel/traverse": "^7.25.3",
|
|
9
|
+
"@friggframework/core": "^1.2.0",
|
|
10
|
+
"@friggframework/test": "^1.2.0",
|
|
11
|
+
"axios": "^1.7.2",
|
|
12
|
+
"commander": "^12.1.0",
|
|
13
|
+
"dotenv": "^16.4.5",
|
|
7
14
|
"eslint": "^8.22.0",
|
|
8
15
|
"eslint-config-prettier": "^8.5.0",
|
|
9
16
|
"eslint-plugin-json": "^3.1.0",
|
|
10
17
|
"eslint-plugin-markdown": "^3.0.0",
|
|
11
18
|
"eslint-plugin-no-only-tests": "^3.0.0",
|
|
12
19
|
"eslint-plugin-yaml": "^0.5.0",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
20
|
+
"fs-extra": "^11.2.0",
|
|
21
|
+
"inquirer": "^10.1.6"
|
|
15
22
|
},
|
|
16
23
|
"devDependencies": {
|
|
17
|
-
"@friggframework/
|
|
18
|
-
"
|
|
19
|
-
"prettier": "^2.7.1"
|
|
24
|
+
"@friggframework/eslint-config": "^1.2.0",
|
|
25
|
+
"@friggframework/prettier-config": "^1.2.0"
|
|
20
26
|
},
|
|
21
27
|
"scripts": {
|
|
22
28
|
"lint:fix": "prettier --write --loglevel error . && eslint . --fix",
|
|
23
29
|
"test": "jest --passWithNoTests # TODO"
|
|
24
30
|
},
|
|
31
|
+
"bin": {
|
|
32
|
+
"frigg": "./frigg-cli/index.js"
|
|
33
|
+
},
|
|
25
34
|
"author": "",
|
|
26
35
|
"license": "MIT",
|
|
27
36
|
"main": "index.js",
|
|
@@ -37,5 +46,5 @@
|
|
|
37
46
|
"publishConfig": {
|
|
38
47
|
"access": "public"
|
|
39
48
|
},
|
|
40
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "9bc9921fe6892cf48ce91702fbf1691646085061"
|
|
41
50
|
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
const {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const {
|
|
2
|
+
Auther,
|
|
3
|
+
ModuleConstants,
|
|
4
|
+
createObjectId,
|
|
5
|
+
connectToDatabase,
|
|
6
|
+
disconnectFromDatabase,
|
|
7
|
+
} = require('@friggframework/core');
|
|
8
|
+
const { createMockApiObject } = require("./mock-integration");
|
|
5
9
|
|
|
6
10
|
|
|
7
11
|
function testAutherDefinition(definition, mocks) {
|
|
@@ -48,7 +52,7 @@ function testAutherDefinition(definition, mocks) {
|
|
|
48
52
|
authCallbackParams = mocks.authorizeResponse || mocks.authorizeParams;
|
|
49
53
|
describe('getAuthorizationRequirements() test', () => {
|
|
50
54
|
it('should return auth requirements', async () => {
|
|
51
|
-
requirements = module.getAuthorizationRequirements();
|
|
55
|
+
requirements = await module.getAuthorizationRequirements();
|
|
52
56
|
expect(requirements).toBeDefined();
|
|
53
57
|
expect(requirements.type).toEqual(ModuleConstants.authType.oauth2);
|
|
54
58
|
expect(requirements.url).toBeDefined();
|
package/test/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const {testDefinitionRequiredAuthMethods} = require('./auther-definition-method-tester');
|
|
2
|
+
const {createMockIntegration, createMockApiObject} = require('./mock-integration');
|
|
3
|
+
const { testAutherDefinition } = require('./auther-definition-tester');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
createMockIntegration,
|
|
8
|
+
createMockApiObject,
|
|
9
|
+
testDefinitionRequiredAuthMethods,
|
|
10
|
+
testAutherDefinition,
|
|
11
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Mocked Requests Set Up for Your API Module
|
|
2
|
+
|
|
3
|
+
## Add Tooling to the Test File
|
|
4
|
+
|
|
5
|
+
Example code:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const Api = require('./Api');
|
|
9
|
+
const { mockApi } = require('../../../../test/utils/mockApi');
|
|
10
|
+
|
|
11
|
+
const MockedApi = mockApi(Api, {
|
|
12
|
+
authenticationMode: 'browser',
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('DemoNock API', async () => {
|
|
16
|
+
before(async function () {
|
|
17
|
+
await MockedApi.initialize({ test: this.test });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
after(async function () {
|
|
21
|
+
await MockedApi.clean({ test: this.test });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('tests a nice thing', async () => {
|
|
25
|
+
const api = await MockedApi.mock();
|
|
26
|
+
const users = await api.getUsers();
|
|
27
|
+
expect(users).to.have.length(2);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Api - the LH API class that is being tested/mocked
|
|
33
|
+
|
|
34
|
+
mockApi - tool to record/playback requests and automate login to API
|
|
35
|
+
|
|
36
|
+
MockedApi - Api class with wrapper around it to record/playback requests and automate login to API. `authenticationMode` sets how to handle authentication when recording requests. Browser means it will require a manual step in the browser (uses Authenticator). Client credentials mode gets a token automcaitcally with machine-to-machine token. Manual means the developer will handle authentication manually, like setting an API key. By default mockApi will use the class name of the mocked API to name the output file. This can be overidden by passing in `displayName`.
|
|
37
|
+
test/recorded-requests - the directory where recorded API requests are saved
|
|
38
|
+
|
|
39
|
+
before - make sure to use the `async function` form here, so we can access mocha's `this` object. The call to `initialize` sets up the tooling for recording/playback
|
|
40
|
+
|
|
41
|
+
after - same here need to use the `async function` form and pass in `this.test` to `clean`. This stops recording/playback and removes all hooks and other tooling from Node's HTTP code.
|
|
42
|
+
|
|
43
|
+
test - instead of using `new Api` in your tests, use `await MockedApi.mock`. Any parameters you would normally pass into `new Api` will be passed through with the call to `mock`. The instance of the Api class that is returned will already be authenticated (unless using manual mode).
|
|
44
|
+
|
|
45
|
+
## Creating new API tests
|
|
46
|
+
|
|
47
|
+
When running your tests while creating them, use `npm --record-apis=demonock test`.
|
|
48
|
+
|
|
49
|
+
This tells the tooling to record the requests made while running the tests for the DemoNockApi class.
|
|
50
|
+
|
|
51
|
+
## Finalizing API tests
|
|
52
|
+
|
|
53
|
+
When satisified with your tests, run `npm test` without adding the `--record-apis` flag to make sure the recorded requests worked. The tests should pass without needing you to perform a login step in the browser.
|
|
54
|
+
|
|
55
|
+
## Tests that fail...
|
|
56
|
+
|
|
57
|
+
Sometimes a "finished" test that was previously working starts to fail. Or, a test passes when it should fail (false negative). There are a few reasons this might happen.
|
|
58
|
+
|
|
59
|
+
### Test fails after code updates
|
|
60
|
+
|
|
61
|
+
If you made updates to the code, this may cause a test to fail. In this case the developer doesn't need to update mocks. To make the test pass, the updated code should be fixed, or the test should be updated to reflect the new data shape.
|
|
62
|
+
|
|
63
|
+
One way to test for this scenario is running `npm test` in the main branch, seeing that all tests pass, and then confirming tests fail in your feature branch. This would show that the test is almost certainly failing due to a change in your branch.
|
|
64
|
+
|
|
65
|
+
### Test should fail but passes
|
|
66
|
+
|
|
67
|
+
If a test is passing, but in the "real world" the code is failing, the API response may have changed. Re-record mocks by running tests with `--record-apis=nockdemo`. Hopefully, one or more tests will now fail. These tests and related code should be updated to work with the new API response data shape.
|
|
68
|
+
|
|
69
|
+
When creating the PR for this update, flag that the mocks were re-recorded so the updated mocks will be sure to be reviewed by another team member as part of code review.
|
|
70
|
+
|
|
71
|
+
### Mocks in error state
|
|
72
|
+
|
|
73
|
+
If the recorded files get messed up when updating (sometimes unavoidable under certain error conditions) just `git restore test/recorded-requests` or delete the file(s) in that directory and re-record.
|
|
74
|
+
|
|
75
|
+
## Miscellaneous Notes
|
|
76
|
+
|
|
77
|
+
More than one API can be set to record mode at a time: `npm --record-apis=activecampaign,hubspot test`
|
|
78
|
+
|
|
79
|
+
You can use .only when --record-apis is passed, but only for the root level test suite. Otherwise an error will be thrown to prevent partial recording of the mocks. .skip is fine to use to skip tests that should not be recorded.
|
|
80
|
+
|
|
81
|
+
CAN UPDATE RECORDED REQUESTS:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
describe('Nock Demo API', () => {
|
|
85
|
+
it('does x');
|
|
86
|
+
it.skip('does y');
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
CANNOT UPDATE RECORDED REQUESTS:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
describe('Nock Demo API', () => {
|
|
94
|
+
it('does x');
|
|
95
|
+
it('does y');
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Caveats
|
|
100
|
+
|
|
101
|
+
- Client credential mode may only work with Crossbeam currently
|
|
102
|
+
- Puppet mode (browser automation) for login is not yet implemented
|