Skip to content

Commit

Permalink
Add result export and update README (#171)
Browse files Browse the repository at this point in the history
* runresult-proxy

* update README

* Update index.ts
  • Loading branch information
mshima committed Feb 28, 2023
1 parent 1824d2b commit 045598c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ $ npm install --save-dev yeoman-generator@xxx yeoman-environment@xxx
Usage:

```js
import helpers from 'yeoman-test';

describe('generator test', () => {
describe('test', () => {
let runResult;
Expand All @@ -36,12 +38,22 @@ describe('generator test', () => {
{} // environment options
)
[.cd(dir)] // runs the test inside a non temporary dir
[.doInDir(dir => {}) // prepares the test dir
[.onTargetDirectory(dir => {}) // prepares the test dir
[.withGenerators([])] // registers additional generators
[.withLookups({})] // runs Environment lookups
[.withOptions({})] // passes options to the generator
[.withLocalConfig({})] // sets the generator config as soon as it is instantiated
[.withAnswers()] // simulates the prompt answers
[.withMockedGenerators(['namespace', ...])] // adds a mocked generator to the namespaces
[.withFiles({
'foo.txt': 'bar',
'test.json', { content: true },
})] // add files to mem-fs
[.withYoRc({ 'generator-foo': { bar: {} } })] // add config to .yo-rc.json
[.withYoRcConfig('generator-foo.bar', { : {} })] // same as above
[.commitFiles()] // commit mem-fs files to disk
[.onGenerator(gen => {})] // do something with the generator
[.onEnvironment(env => {})] // do something with the environment
[.build(runContext => { // instantiates Environment/Generator
[runContext.env...] // does something with the environment
[runContext.generator...] // does something with the generator
Expand All @@ -51,6 +63,7 @@ describe('generator test', () => {
);
afterEach(() => {
if (runResult) {
// Optional if context is executed at a temporary folder
runResult.restore();
}
});
Expand All @@ -68,6 +81,19 @@ describe('generator test', () => {
});
```
Convenience last RunResult instance:
```js
import helpers, { result } from 'yeoman-test';

describe('generator test', () => {
before(() => helpers.run('namespace'));
it('test', () => {
result.assert...;
});
});
```
[See our api documentation](https://yeoman.github.io/yeoman-test) for latest yeoman-test release.
[See our api documentation](https://yeoman.github.io/yeoman-test/5.0.1) for yeoman-test 5.0.1. Use 5.x for yeoman-environment 2.x support.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { default, createHelpers, YeomanTest, type Dependency } from './helpers.j

export { default as RunContext, RunContextBase, type RunContextSettings } from './run-context.js';
export { default as RunResult, type RunResultOptions } from './run-result.js';
export { default as context } from './test-context.js';
export { default as context, result } from './test-context.js';
export { DummyPrompt, TestAdapter } from './adapter.js';
9 changes: 7 additions & 2 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MemFs from 'mem-fs';
import RunResult, { type RunResultOptions } from './run-result.js';
import defaultHelpers, { type GeneratorConstructor, type Dependency, type YeomanTest } from './helpers.js';
import { type DummyPromptOptions } from './adapter.js';
import testContext from './test-context.js';

const { camelCase, kebabCase, merge: lodashMerge, set: lodashSet } = _;

Expand Down Expand Up @@ -150,7 +151,9 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
this.completed = true;
}

return new RunResult(this._createRunResultOptions());
const runResult = new RunResult(this._createRunResultOptions());
testContext.runResult = runResult;
return runResult;
}

// If any event listeners is added, setup event listeners emitters
Expand Down Expand Up @@ -732,6 +735,8 @@ export default class RunContext<GeneratorType extends Generator = Generator>
export class BasicRunContext extends RunContext {
async run(): PromiseRunResult<any> {
await this.prepare();
return new RunResult(this._createRunResultOptions());
const runResult = new RunResult(this._createRunResultOptions());
testContext.runResult = runResult;
return runResult;
}
}
23 changes: 21 additions & 2 deletions src/test-context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import type Generator from 'yeoman-generator';
import type RunContext from './run-context.js';
import type RunResult from './run-result.js';

class TestContext {
runResult?: RunResult;
private runContext?: RunContext<any>;

startNewContext(runContext: RunContext<any>) {
this.runContext?.cleanupTemporaryDir();
this.runContext = runContext;
this.runResult = undefined;
}
}

export default new TestContext();
const testContext = new TestContext();

export default testContext;

const handler2 = {
get(_target, prop, receiver) {
if (testContext.runResult === undefined) {
throw new Error('Last result is missing.');
}

return Reflect.get(testContext.runResult, prop, receiver);
},
};

/**
* Provides a proxy for last executed context result.
*/
export const result: RunResult = new Proxy({}, handler2);
24 changes: 24 additions & 0 deletions test/run-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { stub } from 'sinon';
import RunContext from '../src/run-context.js';
import RunResult from '../src/run-result.js';
import helpers from '../src/helpers.js';
import testContext, { result } from '../src/test-context.js';

describe('run-result', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -244,4 +245,27 @@ describe('run-result', () => {
assert.equal(runContext.envOptions.overridedEnv, 'newOverridedEnv');
});
});
describe('current runResult value', () => {
describe('should proxy methods', () => {
let runResult: RunResult;
beforeEach(() => {
const memFs = MemFs.create();
const memFsEditor = MemFsEditor.create(memFs);
runResult = new RunResult({
memFs,
fs: memFsEditor,
cwd: process.cwd(),
} as any);
runResult.fs.write(path.resolve('test.txt'), 'test content');
runResult.fs.write(path.resolve('test2.txt'), 'test2 content');
testContext.runResult = runResult;
});
for (const method of Object.getOwnPropertyNames(RunResult.prototype)) {
// eslint-disable-next-line @typescript-eslint/no-loop-func
it(`.${method}`, () => {
assert.equal(result.assertFile, runResult.assertFile);
});
}
});
});
});

0 comments on commit 045598c

Please sign in to comment.