Skip to content

Commit

Permalink
Jest setting up
Browse files Browse the repository at this point in the history
Update GeneratedFile to simplify test writing for it

Add basic tests

Remove getFile content and make genetedFile consume string istead of array of string

Add test:watch command

Add coverage and one more test

Update contributing.md with information related to tests
  • Loading branch information
Myrfion committed Nov 14, 2022
1 parent fb38d42 commit cf78d84
Show file tree
Hide file tree
Showing 9 changed files with 4,711 additions and 290 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -21,3 +21,13 @@
<h2>Linting</h2>

<p>Project uses eslint and has the config for it use following command to run it: <code>npm run format</code> before you want to commit something</p>

<h2>Testing</h2>

<p>Izyum uses jest for unit testing and has following npm commands for this purposes</p>

<ul>
<li><code>npm test</code> - runs unit tests</li>
<li><code>npm run test:watch</code> - runs unit tests in a watch mode</li>
<li><code>npm run coverage</code> - shows testing coverage</li>
</ul>
114 changes: 114 additions & 0 deletions __tests__/generated-file.spec.ts
@@ -0,0 +1,114 @@
import GeneratedFile from '../src/generated-file'

jest.mock('fs')

const MOCK_TXT_CONTENT_INPUT = `hello
world`
const MOCK_TXT_WITH_HEADER_INPUT = `Title
`
const MOCK_MD_CONTENT_INPUT = `# heading
Paragraph`
const EXPECTED_HTML_OUTPUT = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filename</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- Your generated content here... -->
<p>hello</p>
</body>
</html>`
const EXPECTED_HTML_FROM_MD_OUTPUT = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test-file</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1 id="heading">heading</h1>
<p>Paragraph</p>
</body>
</html>`
const EXPECTED_HTML_FROM_TXT_WITH_HEADING_OUTPUT = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- Your generated content here... -->
<h1>Title</h1>
</body>
</html>`

describe('Genereted file module', () => {
test('txt to html conversion', () => {
const generatedFile = new GeneratedFile(
MOCK_TXT_CONTENT_INPUT,
'.txt',
'test-file'
)
const htmlOutput = generatedFile.getSerializedHtml()
expect(htmlOutput).toBe(EXPECTED_HTML_OUTPUT)
})

test('txt to html conversion (with title)', () => {
const generatedFile = new GeneratedFile(
MOCK_TXT_WITH_HEADER_INPUT,
'.txt',
'test-file'
)
const htmlOutput = generatedFile.getSerializedHtml()
console.log(htmlOutput)
expect(htmlOutput).toBe(EXPECTED_HTML_FROM_TXT_WITH_HEADING_OUTPUT)
})

test('md to html conversion', () => {
const generatedFile = new GeneratedFile(
MOCK_MD_CONTENT_INPUT,
'.md',
'test-file'
)
const htmlOutput = generatedFile.getSerializedHtml()
expect(htmlOutput).toBe(EXPECTED_HTML_FROM_MD_OUTPUT)
})

test('getWrappedHtml()', () => {
const wrappedHtml = GeneratedFile.getWrappedHtml(
'<p>hello world</p>',
'test-file'
)
expect(wrappedHtml).toBe(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test-file</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>hello world</p>
</body>
</html>`)
})
})
1 change: 1 addition & 0 deletions babel.config.json
@@ -0,0 +1 @@
{ "presets": ["@babel/preset-env"] }
7 changes: 7 additions & 0 deletions jest.config.json
@@ -0,0 +1,7 @@
{
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
}
}

0 comments on commit cf78d84

Please sign in to comment.