 noflo = require 'noflo'
 unless noflo.isBrowser()
   chai = require 'chai' unless chai
-  fs = require 'fs'
-  Canvas = require('noflo-canvas').canvas
-  GetColors = require '../components/GetColors.coffee'
+  path = require 'path'
+  baseDir = path.resolve __dirname, '../'
+  testutils = require './testutils'
 else
-  GetColors = require 'noflo-image/components/GetColors.js'
-
-
-createCanvas = (width, height) ->
-  if noflo.isBrowser()
-    canvas = document.createElement 'canvas'
-    canvas.width = width
-    canvas.height = height
-  else
-    canvas = new Canvas width, height
-  return canvas
-
-getImageData = (name, callback) ->
-  if noflo.isBrowser()
-    id = 'http://localhost:8000/spec/fixtures/'+name
-    image = new Image()
-    image.onload = ->
-      callback image
-    image.src = id
-  else
-    id = 'spec/fixtures/'+name
-    fs.readFile id, (err, data) ->
-      image = new Canvas.Image
-      image.src = data
-      callback image
-  return id
-
-getCanvasWithImage = (name, callback) ->
-  id = getImageData name, (img) ->
-    canvas = createCanvas img.width, img.height
-    canvas.getContext('2d').drawImage(img, 0, 0)
-    callback canvas
-  return id
+  baseDir = '/noflo-image'
+  testutils = require 'noflo-image/spec/testutils.js'
 
 describe 'GetColors component', ->
   c = null

      canvas.once "begingroup", (group) ->
        groups.push group
      canvas.once "data", (canvas) ->
        chai.expect(canvas).to.be.an 'object'
        chai.expect(groups).to.have.length 1
        chai.expect(groups[0]).to.equal id
        done()
      id = getCanvasWithImage input, (canvas) ->
        ins.beginGroup id
        ins.send canvas
        ins.endGroup()

      input = '1x1.gif'
      id = null
      colors.once "data", (colors) ->
        chai.expect(colors).to.have.length 0
        done()
      id = testutils.getCanvasWithImage input, (canvas) ->
        ins.send canvas
  describe 'when given not an image', ->
    it 'should return an error', (done) ->
      error.on "data", (err) ->
        chai.expect(err).to.be.instanceof Error
        done()
      ins.send ''
  describe 'when given null', ->
    it 'should return an error', (done) ->
      error.on "data", (err) ->
        chai.expect(err).to.be.instanceof Error
        done()
      ins.send null
