Welcome to the Linux Foundation Forum!

assert stream run the code, why ?

Hello,

In a single module, i run 2 streams one to UpperCase the entries of the __dirname and a second reverse them. Running the program works fine.

In a separate file i run 2 tests (one for each transform stream), the tests pass BUT it run the program as well, i don t understand that ??

see the main file (running node myfile.js works)

  1. const fs = require('fs')
  2. const { pipeline, Transform, Readable } = require('stream')
  3.  
  4. const createUppercase = () => {
  5. return new Transform({
  6. objectMode: true,
  7. transform(chunk, enc, next) {
  8. // console.log(chunk)
  9. next(null, chunk.name.toUpperCase())
  10. }
  11. })
  12. }
  13.  
  14. const createReverse = () => {
  15. return new Transform({
  16. objectMode: true,
  17. transform(chunk, enc, next) {
  18. // console.log(chunk)
  19. next(null, chunk.name.split("").reverse().join(""))
  20. }
  21. })
  22. }
  23.  
  24. module.exports = { createUppercase, createReverse }
  25.  
  26. // upper case the dir entries
  27. const cu = createUppercase()
  28. fs.opendir(__dirname, (e,d) => {
  29. pipeline(d, cu, e => console.error("pipe error first: ", e))
  30. })
  31. cu.on('data', d => console.log(d))
  32.  
  33. // reverse the dir entries
  34. const cr = createReverse()
  35. fs.opendir(__dirname, (e,d) => {
  36. pipeline(d, cr, e => console.error("pipe error second: ", e))
  37. })
  38. cr.on('data', d => console.log(d))

see the tests file

  1. const { pipeline, Readable } = require('stream')
  2. const assert = require('assert')
  3.  
  4. const { createUppercase, createReverse } = require('./repeat')
  5.  
  6. const test = createUppercase()
  7. const t = Readable.from([{name:'aaa'}, {name:'bbb'}])
  8. t.pipe(test)
  9. test.once('data', d => {
  10. assert.equal(d, 'AAA')
  11. test.once('data', d => {
  12. assert.equal(d, 'BBB')
  13. console.log('test upper passed')
  14. })
  15. })
  16.  
  17. const testR = createReverse()
  18. const rr = Readable.from([{name:'qwerty'}, {name: 'asdfgh'}])
  19. rr.pipe(testR)
  20. testR.once('data', d => {
  21. assert.strictEqual(d, 'ytrewq')
  22. testR.once('data', d => {
  23. assert.strictEqual(d, 'hgfdsa')
  24. console.log('test reverse passed')
  25. })
  26. })

running the test (node test.js) output the following
as you can see the tests pass, but the code is executed as well ..... ??? does someone have an idea why ? Thank you.

  1. test upper passed
  2. test reverse passed
  3. TRY
  4. .GIT
  5. LABS
  6. REPEAT.JS
  7. CLIENT.JS
  8. COURS
  9. TEST.JS
  10. NODESHELLCMD.ODT
  11. yrt
  12. tig.
  13. sbal
  14. sj.taeper
  15. sj.tneilc
  16. sruoc
  17. sj.tset
  18. tdo.dmCllehSedon
  19. pipe error first: undefined
  20. pipe error second: undefined
  21.  

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training