feat(client): support beforeAll in DOM challenge tests (#59001)

This commit is contained in:
Oliver Eyton-Williams
2025-02-28 13:03:18 +01:00
committed by GitHub
parent 1c33d37d8a
commit 96d62330cd
43 changed files with 531 additions and 25 deletions
@@ -0,0 +1,34 @@
# --description--
Paragraph 1
```html
code example
```
# --before-all--
gubbins
# --hints--
First hint
```js
// test code
```
Second hint with <code>code</code>
```js
// more test code
```
Third *hint* with <code>code</code> and `inline code`
```js
// more test code
if(let x of xs) {
console.log(x);
}
```
@@ -0,0 +1,40 @@
# --description--
Paragraph 1
```html
code example
```
# --before-all--
```js
// before all code
function foo() {
return 'bar';
}
foo();
```
# --hints--
First hint
```js
// test code
```
Second hint with <code>code</code>
```js
// more test code
```
Third *hint* with <code>code</code> and `inline code`
```js
// more test code
if(let x of xs) {
console.log(x);
}
```
@@ -0,0 +1,32 @@
# --description--
Paragraph 1
```html
code example
```
# --before-all--
# --hints--
First hint
```js
// test code
```
Second hint with <code>code</code>
```js
// more test code
```
Third *hint* with <code>code</code> and `inline code`
```js
// more test code
if(let x of xs) {
console.log(x);
}
```
@@ -0,0 +1,42 @@
# --description--
Paragraph 1
```html
code example
```
# --before-all--
```js
// before all code
function foo() {
return 'bar';
}
foo();
```
gubbins
# --hints--
First hint
```js
// test code
```
Second hint with <code>code</code>
```js
// more test code
```
Third *hint* with <code>code</code> and `inline code`
```js
// more test code
if(let x of xs) {
console.log(x);
}
```
@@ -0,0 +1,40 @@
# --description--
Paragraph 1
```html
code example
```
# --before-all--
```ts
// before all code
function foo() {
return 'bar';
}
foo();
```
# --hints--
First hint
```js
// test code
```
Second hint with <code>code</code>
```js
// more test code
```
Third *hint* with <code>code</code> and `inline code`
```js
// more test code
if(let x of xs) {
console.log(x);
}
```
+2
View File
@@ -7,6 +7,7 @@ const addFillInTheBlank = require('./plugins/add-fill-in-the-blank');
const addFrontmatter = require('./plugins/add-frontmatter');
const addSeed = require('./plugins/add-seed');
const addSolution = require('./plugins/add-solution');
const addBeforeHook = require('./plugins/add-before-hook');
const addTests = require('./plugins/add-tests');
const addText = require('./plugins/add-text');
const addVideoQuestion = require('./plugins/add-video-question');
@@ -52,6 +53,7 @@ const processor = unified()
.use(addAssignment)
.use(addScene)
.use(addQuizzes)
.use(addBeforeHook)
.use(addTests)
.use(addText, [
'description',
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`add-before-hook plugin should have an output to match the snapshot 1`] = `
{
"hooks": {
"beforeAll": "// before all code
function foo() {
return 'bar';
}
foo();",
},
}
`;
@@ -0,0 +1,33 @@
const { getSection } = require('./utils/get-section');
function plugin() {
return transformer;
function transformer(tree, file) {
const section = getSection(tree, '--before-all--');
if (section.length === 0) return;
if (section.length > 1)
throw Error(
'#--before-all-- section must only contain a single code block'
);
const codeNode = section[0];
if (codeNode.type !== 'code')
throw Error('#--before-all-- section must contain a code block');
if (codeNode.lang !== 'javascript' && codeNode.lang !== 'js')
throw Error('#--before-all-- hook must be written in JavaScript');
const beforeAll = getBeforeAll(codeNode);
file.data.hooks = { beforeAll };
}
}
function getBeforeAll(codeNode) {
const beforeAll = codeNode.value;
return beforeAll;
}
module.exports = plugin;
@@ -0,0 +1,69 @@
const parseFixture = require('../__fixtures__/parse-fixture');
const addBeforeHook = require('./add-before-hook');
describe('add-before-hook plugin', () => {
let withBeforeHookAST,
withEmptyHookAST,
withInvalidHookAST,
withAnotherInvalidHookAST,
withNonJSHookAST;
const plugin = addBeforeHook();
let file = { data: {} };
beforeAll(async () => {
withBeforeHookAST = await parseFixture('with-before-hook.md');
withEmptyHookAST = await parseFixture('with-empty-before-hook.md');
withInvalidHookAST = await parseFixture('with-invalid-before-hook.md');
withAnotherInvalidHookAST = await parseFixture(
'with-another-invalid-before-hook.md'
);
withNonJSHookAST = await parseFixture('with-non-js-before-hook.md');
});
beforeEach(() => {
file = { data: {} };
});
it('returns a function', () => {
expect(typeof plugin).toEqual('function');
});
it('adds a `hooks` property to `file.data`', () => {
plugin(withBeforeHookAST, file);
expect('hooks' in file.data).toBe(true);
});
it('populates `hooks.beforeAll` with the contents of the code block', () => {
plugin(withBeforeHookAST, file);
expect(file.data.hooks.beforeAll).toBe(`// before all code
function foo() {
return 'bar';
}
foo();`);
});
it('should throw an error if the beforeAll section has more than one child', () => {
expect(() => plugin(withInvalidHookAST, file)).toThrow(
`#--before-all-- section must only contain a single code block`
);
});
it('should throw an error if the beforeAll section does not contain a code block', () => {
expect(() => plugin(withAnotherInvalidHookAST, file)).toThrow(
`#--before-all-- section must contain a code block`
);
});
it('should throw an error if the code language is not javascript', () => {
expect(() => plugin(withNonJSHookAST, file)).toThrow(
`#--before-all-- hook must be written in JavaScript`
);
});
it('should have an output to match the snapshot', () => {
plugin(withBeforeHookAST, file);
expect(file.data).toMatchSnapshot();
});
});