Skip to content

Commit bcf8ba1

Browse files
Copilottaylortom
andcommitted
Optimize test performance by reading files once per describe block
Co-authored-by: taylortom <1059083+taylortom@users.noreply.github.com>
1 parent 0c9fe22 commit bcf8ba1

File tree

4 files changed

+32
-60
lines changed

4 files changed

+32
-60
lines changed

tests/AbstractMailTransport.spec.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it } from 'node:test'
1+
import { describe, it, before } from 'node:test'
22
import assert from 'node:assert/strict'
33
import { readFileSync } from 'node:fs'
44
import { join, dirname } from 'node:path'
@@ -7,35 +7,32 @@ import { fileURLToPath } from 'node:url'
77
const __dirname = dirname(fileURLToPath(import.meta.url))
88

99
describe('AbstractMailTransport', () => {
10+
let content
11+
12+
before(() => {
13+
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
14+
content = readFileSync(filePath, 'utf-8')
15+
})
16+
1017
describe('module structure', () => {
1118
it('should have valid JavaScript syntax', () => {
12-
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
13-
const content = readFileSync(filePath, 'utf-8')
1419
assert.ok(content.length > 0)
1520
assert.ok(content.includes('class AbstractMailTransport'))
1621
})
1722

1823
it('should export default class', () => {
19-
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
20-
const content = readFileSync(filePath, 'utf-8')
2124
assert.ok(content.includes('export default AbstractMailTransport'))
2225
})
2326

2427
it('should define send method', () => {
25-
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
26-
const content = readFileSync(filePath, 'utf-8')
2728
assert.ok(content.includes('async send'))
2829
})
2930

3031
it('should define test method', () => {
31-
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
32-
const content = readFileSync(filePath, 'utf-8')
3332
assert.ok(content.includes('async test'))
3433
})
3534

3635
it('should define getConfig method', () => {
37-
const filePath = join(__dirname, '../lib/AbstractMailTransport.js')
38-
const content = readFileSync(filePath, 'utf-8')
3936
assert.ok(content.includes('getConfig'))
4037
})
4138
})

tests/FilesystemTransport.spec.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it } from 'node:test'
1+
import { describe, it, before } from 'node:test'
22
import assert from 'node:assert/strict'
33
import { readFileSync } from 'node:fs'
44
import { join, dirname } from 'node:path'
@@ -7,47 +7,40 @@ import { fileURLToPath } from 'node:url'
77
const __dirname = dirname(fileURLToPath(import.meta.url))
88

99
describe('FilesystemTransport', () => {
10+
let content
11+
12+
before(() => {
13+
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
14+
content = readFileSync(filePath, 'utf-8')
15+
})
16+
1017
describe('module structure', () => {
1118
it('should have valid JavaScript syntax', () => {
12-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
13-
const content = readFileSync(filePath, 'utf-8')
1419
assert.ok(content.length > 0)
1520
assert.ok(content.includes('class FilesystemTransport'))
1621
})
1722

1823
it('should export default class', () => {
19-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
20-
const content = readFileSync(filePath, 'utf-8')
2124
assert.ok(content.includes('export default FilesystemTransport'))
2225
})
2326

2427
it('should extend AbstractMailTransport', () => {
25-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
26-
const content = readFileSync(filePath, 'utf-8')
2728
assert.ok(content.includes('extends AbstractMailTransport'))
2829
})
2930

3031
it('should define name property as filesystem', () => {
31-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
32-
const content = readFileSync(filePath, 'utf-8')
3332
assert.ok(content.includes("name = 'filesystem'"))
3433
})
3534

3635
it('should define outputDir getter', () => {
37-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
38-
const content = readFileSync(filePath, 'utf-8')
3936
assert.ok(content.includes('get outputDir'))
4037
})
4138

4239
it('should define send method', () => {
43-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
44-
const content = readFileSync(filePath, 'utf-8')
4540
assert.ok(content.includes('async send'))
4641
})
4742

4843
it('should define test method', () => {
49-
const filePath = join(__dirname, '../lib/transports/FilesystemTransport.js')
50-
const content = readFileSync(filePath, 'utf-8')
5144
assert.ok(content.includes('async test'))
5245
})
5346
})

tests/MailerModule.spec.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it } from 'node:test'
1+
import { describe, it, before } from 'node:test'
22
import assert from 'node:assert/strict'
33
import { readFileSync } from 'node:fs'
44
import { join, dirname } from 'node:path'
@@ -7,59 +7,48 @@ import { fileURLToPath } from 'node:url'
77
const __dirname = dirname(fileURLToPath(import.meta.url))
88

99
describe('MailerModule', () => {
10+
let content
11+
12+
before(() => {
13+
const filePath = join(__dirname, '../lib/MailerModule.js')
14+
content = readFileSync(filePath, 'utf-8')
15+
})
16+
1017
describe('module structure', () => {
1118
it('should have valid JavaScript syntax', () => {
12-
const filePath = join(__dirname, '../lib/MailerModule.js')
13-
const content = readFileSync(filePath, 'utf-8')
1419
assert.ok(content.length > 0)
1520
assert.ok(content.includes('class MailerModule'))
1621
})
1722

1823
it('should export default class', () => {
19-
const filePath = join(__dirname, '../lib/MailerModule.js')
20-
const content = readFileSync(filePath, 'utf-8')
2124
assert.ok(content.includes('export default MailerModule'))
2225
})
2326

2427
it('should extend AbstractModule', () => {
25-
const filePath = join(__dirname, '../lib/MailerModule.js')
26-
const content = readFileSync(filePath, 'utf-8')
2728
assert.ok(content.includes('extends AbstractModule'))
2829
})
2930

3031
it('should define init method', () => {
31-
const filePath = join(__dirname, '../lib/MailerModule.js')
32-
const content = readFileSync(filePath, 'utf-8')
3332
assert.ok(content.includes('async init'))
3433
})
3534

3635
it('should define registerTransport method', () => {
37-
const filePath = join(__dirname, '../lib/MailerModule.js')
38-
const content = readFileSync(filePath, 'utf-8')
3936
assert.ok(content.includes('registerTransport'))
4037
})
4138

4239
it('should define getTransport method', () => {
43-
const filePath = join(__dirname, '../lib/MailerModule.js')
44-
const content = readFileSync(filePath, 'utf-8')
4540
assert.ok(content.includes('getTransport'))
4641
})
4742

4843
it('should define initTransports method', () => {
49-
const filePath = join(__dirname, '../lib/MailerModule.js')
50-
const content = readFileSync(filePath, 'utf-8')
5144
assert.ok(content.includes('async initTransports'))
5245
})
5346

5447
it('should define send method', () => {
55-
const filePath = join(__dirname, '../lib/MailerModule.js')
56-
const content = readFileSync(filePath, 'utf-8')
5748
assert.ok(content.includes('async send'))
5849
})
5950

6051
it('should define testEmailHandler method', () => {
61-
const filePath = join(__dirname, '../lib/MailerModule.js')
62-
const content = readFileSync(filePath, 'utf-8')
6352
assert.ok(content.includes('async testEmailHandler'))
6453
})
6554
})

tests/SmtpTransport.spec.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it } from 'node:test'
1+
import { describe, it, before } from 'node:test'
22
import assert from 'node:assert/strict'
33
import { readFileSync } from 'node:fs'
44
import { join, dirname } from 'node:path'
@@ -7,47 +7,40 @@ import { fileURLToPath } from 'node:url'
77
const __dirname = dirname(fileURLToPath(import.meta.url))
88

99
describe('SmtpTransport', () => {
10+
let content
11+
12+
before(() => {
13+
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
14+
content = readFileSync(filePath, 'utf-8')
15+
})
16+
1017
describe('module structure', () => {
1118
it('should have valid JavaScript syntax', () => {
12-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
13-
const content = readFileSync(filePath, 'utf-8')
1419
assert.ok(content.length > 0)
1520
assert.ok(content.includes('class SmtpTransport'))
1621
})
1722

1823
it('should export default class', () => {
19-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
20-
const content = readFileSync(filePath, 'utf-8')
2124
assert.ok(content.includes('export default SmtpTransport'))
2225
})
2326

2427
it('should extend AbstractMailTransport', () => {
25-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
26-
const content = readFileSync(filePath, 'utf-8')
2728
assert.ok(content.includes('extends AbstractMailTransport'))
2829
})
2930

3031
it('should define name property as smtp', () => {
31-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
32-
const content = readFileSync(filePath, 'utf-8')
3332
assert.ok(content.includes("name = 'smtp'"))
3433
})
3534

3635
it('should define createTransport method', () => {
37-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
38-
const content = readFileSync(filePath, 'utf-8')
3936
assert.ok(content.includes('createTransport'))
4037
})
4138

4239
it('should define send method', () => {
43-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
44-
const content = readFileSync(filePath, 'utf-8')
4540
assert.ok(content.includes('async send'))
4641
})
4742

4843
it('should define test method', () => {
49-
const filePath = join(__dirname, '../lib/transports/SmtpTransport.js')
50-
const content = readFileSync(filePath, 'utf-8')
5144
assert.ok(content.includes('async test'))
5245
})
5346
})

0 commit comments

Comments
 (0)