Skip to content

Commit

Permalink
feat: add ability to reset injected answers (#10)
Browse files Browse the repository at this point in the history
### what
add the ability to clear injected answers


### why

we use `prompt.inject` to inject answers for testing. however, if a test adds too many injections on accident that don't get used by the test, then this leaks over to the next test which which causes test failures. instead, we should be able to reset the injected answers before each test to make sure it's clean

I believe this is what's causing flaky tests in https://linear.app/withgraphite/issue/GT-10743/look-into-why-tests-involving-promptsinject-fail-in-ci
  • Loading branch information
bngo97 authored Aug 5, 2024
1 parent 095785d commit 5ae570c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ function inject(answers) {
prompt._injected = (prompt._injected || []).concat(answers);
}

function resetInjectedAnswers() {
prompt._injected = [];
}

function override(answers) {
prompt._override = Object.assign({}, answers);
}

module.exports = Object.assign(prompt, { prompt, prompts, inject, override });
module.exports = Object.assign(prompt, { prompt, prompts, inject, resetInjectedAnswers, override });
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@withgraphite/prompts",
"version": "0.0.3",
"version": "0.0.4",
"description": "Lightweight, beautiful and user-friendly prompts",
"license": "MIT",
"repository": {
Expand Down
25 changes: 25 additions & 0 deletions test/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ test('injects', t => {
});
});
});

test('resetInjectedAnswers', t => {
let injected = [ 1, 2, 3 ];
prompt.inject(injected);
t.same(prompt._injected, injected, 'injects array of answers');

prompt({ type: 'text', name:'a', message: 'a message' })
.then(foo => {
t.same(foo, { a:1 }, 'immediately returns object with injected answer');
t.same(prompt._injected, [ 2, 3 ], 'deletes the first answer from internal array');

prompt.resetInjectedAnswers();
t.same(prompt._injected, [], 'deletes everything from internal array');

let secondInjected = [ 4, 5 ];
prompt.inject(secondInjected)
t.same(prompt._injected, secondInjected, 'injects second array of answers');
prompt([{ type: 'text', name:'b', message: 'b message' }, { type: 'text', name:'c', message: 'c message' }])
.then(bar => {
t.same(bar, { b:4, c:5 }, 'immediately handles two prompts at once with new injected answers');
t.same(prompt._injected, [], 'leaves behind empty internal array when exhausted');
t.end();
});
});
});

0 comments on commit 5ae570c

Please sign in to comment.