Skip to content

Commit 8a7cefe

Browse files
add tests and todos
1 parent 0951451 commit 8a7cefe

File tree

5 files changed

+187
-7
lines changed

5 files changed

+187
-7
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TransformJourneyPlugin dynamic journeys 1`] = `
4+
"import { journey, step, monitor } from '../../';
5+
6+
journey('j1', () => {});
7+
8+
const createJourney = (name) => {
9+
journey(name, () => {
10+
monitor.use({ id: 'duplicate id' });
11+
step("step1", () => {});
12+
});
13+
};
14+
15+
createJourney('j2');"
16+
`;
17+
18+
exports[`TransformJourneyPlugin dynamic journeys 2`] = `
19+
"import { journey, step, monitor } from '../../';
20+
21+
22+
23+
const createJourney = (name) => {
24+
journey(name, () => {
25+
monitor.use({ id: 'duplicate id' });
26+
step("step1", () => {});
27+
});
28+
};
29+
30+
createJourney('j2');"
31+
`;
32+
33+
exports[`TransformJourneyPlugin dynamic journeys 3`] = `
34+
"import { journey, step, monitor } from '../../';
35+
36+
journey('j1', () => {});
37+
38+
const createJourney = (name) => {
39+
journey(name, () => {
40+
monitor.use({ id: 'duplicate id' });
41+
step("step1", () => {});
42+
});
43+
};
44+
45+
createJourney('j2');"
46+
`;
47+
48+
exports[`TransformJourneyPlugin static journeys 1`] = `
49+
"import { journey, step, monitor } from '../../';
50+
journey('j1', () => {
51+
monitor.use({ id: 'duplicate id' });
52+
});
53+
54+
function util() {}"
55+
`;
56+
57+
exports[`TransformJourneyPlugin static journeys 2`] = `
58+
"import { journey, step, monitor } from '../../';
59+
60+
61+
62+
63+
function util() {}
64+
65+
journey('j2', () => {});"
66+
`;
67+
68+
exports[`TransformJourneyPlugin static journeys 3`] = `
69+
"import { journey, step, monitor } from '../../';
70+
journey('j1', () => {
71+
monitor.use({ id: 'duplicate id' });
72+
});
73+
74+
function util() {}
75+
76+
journey('j2', () => {});"
77+
`;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2020-present, Elastic NV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
import { join } from 'path';
27+
import { transform } from '../../src/push/transform-journey';
28+
import { mkdir, rm, writeFile } from 'fs/promises';
29+
30+
describe('TransformJourneyPlugin', () => {
31+
const PROJECT_DIR = join(__dirname, 'test-transform');
32+
const journeyFile = join(PROJECT_DIR, 'transform.journey.ts');
33+
34+
beforeAll(async () => {
35+
await mkdir(PROJECT_DIR, { recursive: true });
36+
});
37+
38+
afterAll(async () => {
39+
await rm(PROJECT_DIR, { recursive: true });
40+
});
41+
it('static journeys', async () => {
42+
await writeFile(
43+
journeyFile,
44+
`import {journey, step, monitor} from '../../';
45+
journey('j1', () => {
46+
monitor.use({ id: 'duplicate id' });
47+
});
48+
49+
function util(){}
50+
51+
journey('j2', () => {});`
52+
);
53+
54+
const res1 = await transform(journeyFile, 'j1');
55+
expect(res1?.code).toMatchSnapshot();
56+
const res2 = await transform(journeyFile, 'j2');
57+
expect(res2?.code).toMatchSnapshot();
58+
const res3 = await transform(journeyFile, '');
59+
expect(res3?.code).toMatchSnapshot();
60+
});
61+
62+
it('dynamic journeys', async () => {
63+
await writeFile(
64+
journeyFile,
65+
`import {journey, step, monitor} from '../../';
66+
67+
journey('j1', () => {});
68+
69+
const createJourney = (name) => {
70+
journey(name, () => {
71+
monitor.use({ id: 'duplicate id' });
72+
step("step1", () => {})
73+
});
74+
};
75+
76+
createJourney('j2');
77+
`
78+
);
79+
80+
const res1 = await transform(journeyFile, 'j1');
81+
expect(res1?.code).toMatchSnapshot();
82+
const res2 = await transform(journeyFile, 'j2');
83+
expect(res2?.code).toMatchSnapshot();
84+
const res3 = await transform(journeyFile, '');
85+
expect(res3?.code).toMatchSnapshot();
86+
});
87+
});

src/push/bundler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export class Bundler {
4242
moduleMap = new Map<string, string>();
4343
constructor() {}
4444

45-
async prepare(absPath: string, id: string) {
45+
async prepare(absPath: string, name: string) {
4646
const original = await readFile(absPath, 'utf-8');
47-
const { code } = await transform(absPath, id);
47+
const { code } = await transform(absPath, name);
4848
await writeFile(absPath, code);
4949

5050
const options: esbuild.BuildOptions = {
@@ -89,8 +89,8 @@ export class Bundler {
8989
});
9090
}
9191

92-
async build(entry: string, output: string, id: string) {
93-
await this.prepare(entry, id);
92+
async build(entry: string, name: string, output: string) {
93+
await this.prepare(entry, name);
9494
await this.zip(output);
9595
const data = await this.encode(output);
9696
await this.checkSize(output);

src/push/monitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ export async function buildMonitorSchema(monitors: Monitor[], isV2: boolean) {
137137
...config,
138138
locations: translateLocation(config.locations),
139139
};
140+
140141
if (type === 'browser') {
141142
const outPath = join(bundlePath, config.name + '.zip');
142-
const content = await bundler.build(source.file, outPath, config.name);
143+
const content = await bundler.build(source.file, config.name, outPath);
143144
monitor.setContent(content);
144145
Object.assign(schema, { content, filter });
145146
}

src/push/transform-journey.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,25 @@ export function JourneyTransformPlugin(
3434
{ types: t }: { types: typeof types },
3535
opts: JourneyPluginOptions
3636
): PluginObj {
37+
// TODO: Perform a Program level visit to check if import/require declarations are tampered
38+
// If so, dont perform any transformation
39+
// Ex: import * as synthetics from '@elastic/synthetics'
40+
// synthetics.journey('name', () => {})
3741
return {
3842
name: 'transform-journeys',
3943
visitor: {
4044
CallExpression(path) {
45+
if (!path.parentPath.isExpressionStatement()) {
46+
return;
47+
}
4148
const { callee } = path.node;
4249
if (t.isIdentifier(callee) && callee.name === 'journey') {
4350
const args = path.node.arguments;
4451
if (!t.isStringLiteral(args[0])) {
4552
return;
4653
}
47-
if (args[0].value === opts.name) {
54+
// TODO: Compare based on function body, solid than relying on name
55+
if (opts.name == '' || args[0].value === opts.name) {
4856
path.skip();
4957
} else {
5058
path.parentPath.remove();
@@ -61,6 +69,13 @@ export async function transform(absPath: string, journeyName: string) {
6169
retainLines: true,
6270
babelrc: false,
6371
configFile: false,
64-
plugins: [[JourneyTransformPlugin, { name: journeyName }]],
72+
plugins: [
73+
[
74+
JourneyTransformPlugin,
75+
{
76+
name: journeyName,
77+
},
78+
],
79+
],
6580
});
6681
}

0 commit comments

Comments
 (0)