microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7b48f26eb8caae7ed9cc1a09249195dfed76fa76

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

test/resources/recordingsHelper.ts

95lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as fs from "fs";
5import * as path from "path";
6
7const RECORDINGS_ROOT = path.resolve(__dirname, "processExecutionsRecordings");
8
9interface TestUsingRecording {
10 (expectation: string, recordingNames: string[], assertion?: () => void): Mocha.Test;
11 (
12 expectation: string,
13 recordingNames: string[],
14 assertion?: (done: Mocha.Done) => void,
15 ): Mocha.Test;
16 only(expectation: string, recordingNames: string[], assertion?: () => void): Mocha.Test;
17 only(
18 expectation: string,
19 recordingNames: string[],
20 assertion?: (done: Mocha.Done) => void,
21 ): Mocha.Test;
22 skip(expectation: string, recordingNames: string[], assertion?: () => void): void;
23 skip(
24 expectation: string,
25 recordingNames: string[],
26 assertion?: (done: Mocha.Done) => void,
27 ): void;
28}
29
30export interface IRecordingConsumer {
31 loadRecordingFromName(recordingName: string): Promise<void>;
32 loadRecordingFromString(recordingName: string): Promise<void>;
33}
34
35/* This class makes it easy to create a test using a recording. Recommended usage is:
36 const testWithRecordings = new RecordingsHelper(() => recordingConsumer).test;
37 testWithRecordings("expects to do some test thing",
38 [
39 "path/to/recording",
40 "path/to/recording"
41 ], () => {
42 // test code here
43 });
44*/
45export class RecordingsHelper {
46 public test!: TestUsingRecording;
47
48 private recordings: { [name: string]: string };
49
50 constructor(private getRecordingConsumer: () => IRecordingConsumer) {
51 this.recordings = {};
52 this.initializeTest();
53 }
54
55 private initializeTest(): void {
56 this.test = <TestUsingRecording>((
57 testName: string,
58 recordingNames: string[],
59 code: () => Promise<void>,
60 ): void => {
61 if (code.length !== 0) {
62 // Check how many arguments the function has
63 throw new RangeError(
64 "(done: Mocha.Done) parameter is not supported. Please return a promise instead.",
65 );
66 }
67 const recordingsHelper = this;
68 recordingNames.forEach(recordingName => {
69 let recording: string = this.recordings[recordingName];
70 if (!recording) {
71 recording = fs.readFileSync(
72 path.resolve(RECORDINGS_ROOT, recordingName) + ".json",
73 "utf8",
74 );
75 this.recordings[recordingName] = recording;
76 }
77
78 test(`${testName} using recording ${recordingName}`, function () {
79 // We use function () because we need the this pointer
80 return recordingsHelper
81 .getRecordingConsumer()
82 .loadRecordingFromString(recording)
83 .then(code.bind(test));
84 });
85 });
86 });
87 this.test.skip = (
88 expectation: string,
89 recordingNames: string[],
90 assertion?: (done: Mocha.Done) => void,
91 ) => {
92 test.skip(expectation, assertion);
93 };
94 }
95}
96